Saturday 26 February 2022

Exploring logback configuration file

A typical logback configuration file contains a <configuration> element, which contains,

a.   <root> elements

b.   Zero or more appender elements

c.    Zero or more logger elements

 

<logger> element

A logger is configured using <logger> element.

 

Example

<logger name="com.sample.app" level="WARN"/>

 

A logger element takes

a.   name attribute (mandatory)

b.   an optional level attribute, value of the level attribute can be TRACE, DEBUG, INFO, WARN, ERROR, ALL or OFF.

c.    an optional additivity attribute

 

<logger name="com.sample.app" level="WARN">
  <appender-ref ref="STDOUT" />
  <appender-ref ref="FILE" />
</logger> 

<root> element

<root> element is used to configure the root logger. Similar to a <logger> element, a <root> element can contain one or more <appender-ref> elements.

<root level="INFO">
    <appender-ref ref="STDOUT" />
</root>

<root> element support only single attribute ‘level’ which is used to set the logging level of root logger.

 

Can I configure separate logging levels to different loggers?

Yes

 

Example

<logger name="com.sample.app" level="WARN">
  <appender-ref ref="STDOUT" />
  <appender-ref ref="FILE" />
</logger>

<logger name="com.sample.app.util" level="ERROR">
  <appender-ref ref="STDOUT" />
  <appender-ref ref="FILE" />
</logger>

In the above example, I set the

a.   logging level ERROR to "com.sample.app.util" and

b.   logging level WARN to "com.sample.app"

 

<appender> element

An appender is configured using <appender> element. An appender element takes two mandatory attributes.

a.   name : Specifies the name of appender

b.   class: Specifies the fully qualified name of the appender class to instantiate.

 

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
  <file>/Users/Shared/logback/myApp.log</file>

  <encoder>
    <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n
    </pattern>
  </encoder>
</appender>

An <appender> element can contain <layout> <filter> and <encoder> elements. I will explain these in my later posts.

 

Find the below working application.

 

Step 1: Create new maven project ‘logback-console-file-appender’.

 

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-console-file-appender</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 under src/main/resources folder.

 

logback.xml

<configuration scan="true" scanPeriod="15 seconds">


  <appender name="STDOUT"
    class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
      </pattern>
    </encoder>
  </appender>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>/Users/Shared/logback/myApp.log</file>

    <encoder>
      <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n
      </pattern>
    </encoder>
  </appender>


  <logger name="com.sample.app" level="WARN">
    <appender-ref ref="FILE" />
  </logger>

  <logger name="com.sample.app.util" level="ERROR" />

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

Step 4: Define DemoUtil class.

 

DemoUtil.java

package com.sample.app.util;

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

public class DemoUtil {
  private static Logger logger = LoggerFactory.getLogger(DemoUtil.class);

  public static void print() {

    logger.trace("Trace message");
    logger.debug("Debug message");
    logger.info("Info message");
    logger.warn("Warning message");
    logger.error("Error message");
  }
}


Step 5: Define HelloWorld class.

 

HelloWorld.java


package com.sample.app;

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

import com.sample.app.util.DemoUtil;

public class HelloWorld {
  private static Logger logger = LoggerFactory.getLogger(HelloWorld.class);

  public static void main(String[] args) {

    logger.trace("Trace message");
    logger.debug("Debug message");
    logger.info("Info message");
    logger.warn("Warning message");
    logger.error("Error message");
    
    DemoUtil.print();

  }
}

Total project structure looks like below.





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

15:38:59.036 [main] WARN  com.sample.app.HelloWorld - Warning message
15:38:59.039 [main] ERROR com.sample.app.HelloWorld - Error message
15:38:59.039 [main] ERROR com.sample.app.util.DemoUtil - Error message

You can observe that the same log messages are logged to the myApp.log file.

$ls
myApp.log
$
$cat myApp.log 
2022-02-21 15:38:59,036 WARN [main] c.s.a.HelloWorld [HelloWorld.java:16] Warning message
2022-02-21 15:38:59,039 ERROR [main] c.s.a.HelloWorld [HelloWorld.java:17] Error message
2022-02-21 15:38:59,039 ERROR [main] c.s.a.u.DemoUtil [DemoUtil.java:15] Error message

You can download complete working application from below link.

https://github.com/harikrishna553/java-libs/tree/master/logback/logback-console-file-appender








Previous                                                 Next                                                 Home

No comments:

Post a Comment