Configure logging

Describes how the default logback framework is configured.

Reference system logging in general

The reference code uses SLF4J for logging. This enables a concrete installation to use a logging backend like Log4J, Logback, JCL etc. by just placing the relevant jar file on the class path. See SLF4J documentation for details.

The default logging backend used is the modern logback framework, which is replacing log4j.

Configuring the logback logger

Logback can run without a log file and will then log to the console. If it finds a logback.xml file on the classpath it will use this. Below is show a standard logfile as used in the reference code:

<configuration scan="true">
  <property name="DEFAULT_LOG_FILE" value="logs/pillar.log" />
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <Pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</Pattern>
    </encoder>
  </appender>
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <!-- Will generate a logfile at ${LOG_FILE} if defined, else ${DEFAULT_LOG_FILE}-->
    <file>${LOG_FILE:-${DEFAULT_LOG_FILE}}</file>
    <encoder>
      <Pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</Pattern>
    </encoder>
  </appender>
  <root level="WARN">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
  </root>
  <logger name="org.bitrepository" level="INFO" additivity="false">
    <appender-ref ref="FILE" />
  </logger>
</configuration>

Log configuration explained:

  • scan="true" will tell logback to scan the log configuration for changes and load these if found. 
  •  <property name="DEFAULT_LOG_FILE" value="logs/pillar.log" /> : Defines a default log output file location.
  •  <Pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</Pattern>: Defines the format of the log entries. the format here is: timestamp level logging class message
  • <file>${LOG_FILE:-${DEFAULT_LOG_FILE}}</file> : Will generate a logfile at ${LOG_FILE} if defined, else ${DEFAULT_LOG_FILE}. This means that the location of the log file can be defined on the commandline, which enables different application to use the same log file and still produce separate logfiles.
  • <root level="WARN">: Defines the default logger with appenders.
  • <logger name="org.bitrepository" level="INFO" additivity="false">: Defines a more detailed logger to use for the bitrepository code. Will only write the low level logs to file.