Logback Configuration in Spring Boot Applications

Logback is a robust, fast, and flexible logging framework developed by the original author of log4j. Its official site is http://logback.qos.ch. It is structured into three core modules:

  • logback-core: foundational module supporting the other two.
  • logback-classic: an enhanced version of log4j, fully implementing the SLF4J API, enabling seamless substitution with other logging frameworks (e.g., java.util.logging or log4j).
  • logback-access: integrates with servlet containers to provide HTTP-based access logging.

In Spring Boot applications, Logback serves as the default logging backend. The dependency spring-boot-starter-logging—automatically included in spring-boot-starter—brings in Logback, offering performance and footprint advantages over competing frameworks.

Core Logback Concepts

  • Logger, Appender, and Layout
  • Logger: captures log events and assigns them contextual metadata, including name and effective level.
  • Appender: defines destinations for log output—such as console, file, database, syslog, or JMS.
  • Layout: formats log events into strings using customizable patterns.
  • Logger Context
  • All loggers belong to a single LoggerContext, forming a hierarchical tree. The LoggerContext manages logger lifecycle and name resolution via org.slf4j.LoggerFactory.getLogger(String).
  • Level Inheritance & Effective Level
  • Logger levels: TRACE, DEBUG, INFO, WARN, ERROR (in ascending order). Unassigned loggers inherit from the nearest ancestor with a defined level. The root logger defaults to DEBUG.
  • A logging request at level p executes only if p ≥ q, where q is the logger’s effective level.

Default Configuration Behavior

Spring Boot looks for Logback configuration in the following order:

  1. logback-spring.xml
  2. logback.xml
  3. Fallback to minimal BasicConfigurator, which outputs DEBUG-level logs to the console using the format: %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n

If neither file is present, Spring Boot enables console-only logging.

Structured Configuration (logback-spring.xml)

Example root element:

<configuration scan="true" scanPeriod="60 seconds" debug="false">
  ...
</configuration>

Key Subelements

  • <contextName>: Assigns a unique identifier to the logger context (e.g., application name). Immutable after declaration.
<contextName>my-app</contextName>

  • <property>: Declares reusable variables.
<property name="LOG_DIR" value="/var/log/myapp" />
<property name="APP_NAME" value="MyService" />

Referenced via ${LOG_DIR} or ${APP_NAME} elsewhere.

  • <timestamp>: Injects time-based values.
<timestamp key="timestampKey" datePattern="yyyy-MM-dd"/>
<property name="DATE" value="${timestampKey}" />

  • <appender>: Configures output targets.

ConsoleAppender Example:

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

RollingFileAppender Example (with time- and size-based rotation):

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <file>${LOG_DIR}/app-current.log</file>
  <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <fileNamePattern>${LOG_DIR}/app-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
    <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
      <maxFileSize>10MB</maxFileSize>
    </timeBasedFileNamingAndTriggeringPolicy>
    <maxHistory>30</maxHistory>
  </rollingPolicy>
  <encoder>
    <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{35} - %msg%n</pattern>
  </encoder>
</appender>

Other appender types include FileAppender (simple append), SocketAppender, and database appenders.

  • <root>: Default logger for all unqualified log requests.
<root level="INFO">
  <appender-ref ref="CONSOLE"/>
  <appender-ref ref="FILE"/>
</root>

  • <logger>: Enables per-package or per-class customization.
<logger name="org.hibernate.SQL" level="DEBUG"/>
<logger name="com.example.service" level="TRACE"/>

Custom Setup Example

To override Spring Boot’s default logging, exclude the default logging starter and explicitly include Logback modules:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.4.14</version>
</dependency>

Then define src/main/resources/logback.xml:

<configuration>
  <property name="APP_NAME" value="demo-service"/>
  <property name="LOG_PATH" value="${user.home}/${APP_NAME}/logs"/>

  <!-- Console Output -->
  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%cyan(%d{HH:mm:ss.SSS}) %highlight(%-5level) [%blue(%thread)] %gray(%logger{36}) - %msg%n</pattern>
    </encoder>
  </appender>

  <!-- File Output with Time-Based Rotation -->
  <appender name="APP_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_PATH}/application.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>${LOG_PATH}/archive/app-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
      <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
        <maxFileSize>20MB</maxFileSize>
      </timeBasedFileNamingAndTriggeringPolicy>
      <maxHistory>14</maxHistory>
    </rollingPolicy>
    <encoder>
      <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{40} - %msg%n</pattern>
    </encoder>
  </appender>

  <!-- Root Logger -->
  <root level="INFO">
    <appender-ref ref="CONSOLE"/>
    <appender-ref ref="APP_LOG"/>
  </root>

  <!-- Adjust specific packages -->
  <logger name="com.example.controller" level="DEBUG"/>
</configuration>

This setup implements console colorized logging and automated file archival with time- and size-triggered rotation.

Tags: spring-boot logback logging-configuration rollingfileappender SLF4J

Posted on Wed, 19 Aug 2026 17:00:27 +0000 by Pepe