Using Logback and SLF4J for Logging in Spring Boot Applications
This article explains why Logback is the preferred logging framework for Spring Boot, shows how to add the necessary dependencies, configure logback‑spring.xml with appenders, encoders, filters and rolling policies, and provides best practices for efficient log statements in Java code.
Introduction
Logging is essential in any project. The most popular Java logging frameworks are Log4j and Logback; Logback is the successor of Log4j and works natively with SLF4J, which provides a common logging façade.
Why Use Logback
Higher efficiency and better runtime adaptability.
Native SLF4J support and is the default logging framework in Spring Boot.
Getting Started
1. Add Dependency
Add the starter dependency (Spring Boot includes Logback by default):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>In practice you only need spring-boot-starter-web , which pulls in the logging starter automatically.
2. Default Configuration
By default Spring Boot logs to the console. To write to a file, set logging.file or logging.path in application.properties . Note that both cannot be used simultaneously; logging.file takes precedence.
logging.file=my.log logging.path=/var/log3. Detailed logback-spring.xml
Spring Boot recommends using a file named logback-spring.xml placed under src/main/resources . The basic structure is:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight(%-5level) --- [%15.15(%thread)] %cyan(%-40.40(%logger{40})) : %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
...
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>The file defines several key elements:
<appender> : defines where logs are written (console, rolling file, etc.).
<logger> : configures logging level for specific packages and can reference multiple appenders.
<root> : the fallback logger for the whole application.
Appender Types
Two most important appenders are ConsoleAppender (writes to console) and RollingFileAppender (writes to a file and rolls based on time/size). Rolling policies such as SizeAndTimeBasedRollingPolicy allow log rotation by date and size, with parameters like maxHistory and totalSizeCap .
<appender name="info_log" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>logs/project_info.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>logs/project_info.%d.%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
<maxFileSize>10MB</maxFileSize>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level --- [%15.15(%thread)] %-40.40(%logger{40}) : %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>Filters
Filters such as LevelFilter and ThresholdFilter control which log levels are accepted. Example: only INFO level logs are printed.
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>Best Practices for Log Statements
Avoid constructing log messages when the log level is disabled. Use placeholders instead of string concatenation:
logger.debug("The entry is {}", entry);This defers message formatting until the logger decides to actually log, improving performance by up to 30× when the log level is disabled.
Logging Exceptions
To include stack traces, pass the exception as a second argument:
logger.error("Program exception, details: {}", e.getLocalizedMessage(), e);This ensures the full stack trace is recorded.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.