Java Logger - Logback

> Procedural Languages > Java > Java - (Debugging) Logger, Logging system

1 - About

logback is a logging system.

Advertising

3 - Configuration

3.1 - example

logback.xml
<configuration>
 
  <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>
 
  <logger name="com.mchange.v2" level="warn"/>
  <logger name="io.netty" level="warn"/>
  <logger name="io.vertx" level="info"/>
  <logger name="net.bytle" level="warn"/>
 
  <root level="debug">
    <appender-ref ref="STDOUT"/>
  </root>
 
</configuration>

where, there is:

  • one appender that write to the console with the message format %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
  • several logger that gets their level.
  • the root logger gets the debug level and the console appender. All other logger will inherit from it.

3.2 - Set the configuration file

  • at the command line
java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1
  • in the code
import ch.qos.logback.classic.util.ContextInitializer;
 
public class ServerMain {
    public static void main(String args[]) throws IOException, InterruptedException {
       // must be set before the first call to  LoggerFactory.getLogger();
       // ContextInitializer.CONFIG_FILE_PROPERTY is set to "logback.configurationFile"
       System.setProperty(ContextInitializer.CONFIG_FILE_PROPERTY, "/path/to/config.xml");
       ...
    }   
}

3.2.1 - Rescan

<configuration scan="true" scanPeriod="30 seconds" > 
  ... 
</configuration> 

3.3 - Logger

3.3.1 - Parameterized message

With a parameterized message only after evaluating whether to log or not, and only if the decision is positive, will the logger implementation format the message and replace the '{}' pair with the string value of entry. In other words, this form does not incur the cost of parameter construction when the log statement is disabled.

logger.debug("The entry is {}.", entry);
 
// or
 
logger.debug("The new entry is {}. It replaces {}.", entry, oldEntry);
 
// or
 
Object[] paramArray = {newVal, below, above};
logger.debug("Value {} was inserted between {} and {}.", paramArray);
Advertising

3.3.2 - Configuring logger

Configuring loggers, or the <logger> element

One appender use on the root and chapters.configuration logger

<configuration>
 
  <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>
 
  <logger name="chapters.configuration">
    <appender-ref ref="STDOUT" />
  </logger>
 
  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

4 - Appender

4.1 - File Appender

Doc

<configuration>
  <appender name="fileAppender" class="ch.qos.logback.core.rolling.FileAppender">
    <file>my.log</file>
    <encoder>
      <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>
  [...]
</configuration>

where:

Advertising

5 - Documentation / Reference