Logback Boilerplate

A useful starting place logback setup.

I needed a way to roll logs not only by date, but also by size, to prevent behemoth log files from being generated. The marvellousSizeAndTimeBasedFNATP triggering policy (which comes out of the box) will gzip roll logs based on date, and then size too. So your logs directory ends up looking something like this:


Here’s a sample logback.xml configuration that:

  • Determines the target file system destination from a JNDI property lookup from the container. Basically an environment variable.
  • Enables the polling of configuration changes using the scanPeriod property.
  • Will roll logs by size and time.
  • Shows off a named logger called performance that makes use of a custom encoder pattern that injects contextual properties defined at runtime.
  • Will email ERRORS or higher via the SMTPAppender. The hostname of the server the JVM is running on will be injected into the subject line using $HOSTNAME.


<configuration scan="true" scanPeriod="60 seconds" debug="true">
  <insertFromJNDI env-entry-name="cell/persistent/var/logdir" as="LOG_DIRECTORY" />

  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">

  <appender name="PERFORMANCE-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">

  <appender name="SMTP" class="ch.qos.logback.classic.net.SMTPAppender">
    <Subject>Production [${HOSTNAME}] Application Problem</Subject>
    <layout class="ch.qos.logback.classic.PatternLayout">
      <pattern>%d{HH:mm:ss.SSS} %-55(%X{user} %level [%thread] %logger{20}) - %msg%n</pattern>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">


  <logger name="performance" level="INFO" additivity="false">
      <appender-ref ref="PERFORMANCE-FILE"/>

  <root level="DEBUG">
    <appender-ref ref="FILE" />
    <appender-ref ref="SMTP" />


public class FooService {
  private static final Logger logger = LoggerFactory.getLogger(FooService.class);
  private static final Logger performanceLogger = LoggerFactory.getLogger("performance");

  public void shipOrder(CustomerDTO customerDto) {
    // log context properties

    MDC.put("customerId", customerDto.getTravellerId());
    MDC.put("customerSurname", customerDto.getApplianceId());
    MDC.put("processStage", "shippment");
    performanceLogger.info("shipOrder startTime " + System.currentMillis());
    performanceLogger.info(shipOrder endTime " + System.currentMillis());

Last thing. If you need to externalise the logging configuration from the core logback.xml, its super easy. This can be useful for EE application that are bundled into a fat EAR.

The core logback.xml that gets bundled into the EAR would look something like this. This is really where the scanPeriod shines, periodically polling the externalised configuration file for changes:

<configuration scan="true" scanPeriod="60 seconds" debug="true">
  <include file="/var/logs/fooapp/cfg/logback-fooapp.xml"/> 

Then in a file named logback-fooapp.xml go nuts, the only difference is you need to enclose everything in an included tag:

  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">