Table of Contents

About

logback is a logging system.

Configuration

http://logback.qos.ch/manual/configuration.html

example

<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.

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");
       ...
    }   
}

Rescan

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

Logger

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);

Configuring logger

Configuring loggers, or the

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>
Appender

Java - Appender (or Handlers) in Logback

Doc

List:

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:

Documentation / Reference