Friday 25 February 2022

Configure logback with a configuration file

You can configure logback either programmatically or via configuration.

 

When an application launched,

 

a.   Logback tries to find a file called logback-test.xml in the classpath.

b.   If no such file is found, it checks for the file logback.xml in the classpath.

c.    If the first two conditions do not met, then logback use the ServiceLoader facility to resolve the implementation of com.qos.logback.classic.spi.Configurator interface.

d.   If not condition a, b, c are met, then logback configures with default settings which will cause logging output to be directed to the console.

 

Follow below step-by-step procedure to build complete working application.

 

Step 1: Create new maven project ‘logback-configuration-hello-world’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>logback-configuration-hello-world</artifactId>
	<version>1</version>

	<dependencies>

		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.2.10</version>
		</dependency>


		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>1.2.10</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.36</version>
		</dependency>


	</dependencies>
</project>

 

Step 3: Create logback.xml file in src/main/resources folder.

 

logback.xml

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="warn">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

 

Step 4: Define HelloWorld class.

 

HelloWorld.java

 

package com.sample.app;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {

  public static void main(String[] args) {

    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.trace("Trace message");
    logger.debug("Debug message");
    logger.info("Info message");
    logger.warn("Warning message");
    logger.error("Error message");

  }
}

 

Total project structure looks like below.



 

Run HelloWorld.java, you will see below messages in the console.

20:59:00.287 [main] WARN  com.sample.app.HelloWorld - Warning message
20:59:00.289 [main] ERROR com.sample.app.HelloWorld - Error message

You can download complete working application from below link.

https://github.com/harikrishna553/java-libs/tree/master/logback/logback-configuration-hello-world


Previous                                                 Next                                                 Home

No comments:

Post a Comment