How to Use Logback in Spring Boot: Setup and Example
Spring Boot uses
Logback as its default logging framework. To use it, add a logback-spring.xml file in your src/main/resources folder to customize logging behavior, or rely on default settings. You can configure log levels, appenders, and patterns in this XML file to control how logs are recorded and displayed.Syntax
The main configuration for Logback in Spring Boot is done in a logback-spring.xml file placed in src/main/resources. This XML file defines appenders (where logs go), loggers (which parts of your app log), and patterns (how logs look).
<configuration>: Root element for Logback config.<appender>: Defines output targets like console or files.<logger>: Sets log level and behavior for specific packages or classes.<root>: Defines default logging level and appenders.
xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="CONSOLE" 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.example" level="DEBUG"/> <root level="INFO"> <appender-ref ref="CONSOLE" /> </root> </configuration>
Example
This example shows a simple Spring Boot application with a logback-spring.xml file that logs messages to the console. It sets the root log level to INFO and enables DEBUG logging for the com.example package.
java + xml
package com.example.demo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class); public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); logger.info("Application started."); logger.debug("Debugging application startup."); } } --- src/main/resources/logback-spring.xml --- <?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="CONSOLE" 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.example" level="DEBUG"/> <root level="INFO"> <appender-ref ref="CONSOLE" /> </root> </configuration>
Output
12:00:00.123 [main] INFO com.example.demo.DemoApplication - Application started.
12:00:00.124 [main] DEBUG com.example.demo.DemoApplication - Debugging application startup.
Common Pitfalls
Common mistakes when using Logback in Spring Boot include:
- Placing the configuration file with the wrong name (should be
logback-spring.xmlfor Spring Boot to detect it properly). - Using
logback.xmlinstead, which disables some Spring Boot features like profile-specific logging. - Not setting the correct log levels, causing important logs to be hidden.
- Forgetting to include appenders, resulting in no visible logs.
Always use logback-spring.xml to leverage Spring Boot's advanced logging support.
xml
Wrong way (file named logback.xml): <?xml version="1.0" encoding="UTF-8"?> <configuration> <!-- This disables Spring Boot profile support --> </configuration> Right way (file named logback-spring.xml): <?xml version="1.0" encoding="UTF-8"?> <configuration> <!-- Enables Spring Boot profile support --> </configuration>
Quick Reference
Tips for using Logback in Spring Boot:
- Use
logback-spring.xmlfor configuration. - Define appenders like
ConsoleAppenderorFileAppenderto control log output. - Set log levels per package or class with
<logger>. - Use
<root>to set default log level and appenders. - Use patterns to format log messages for readability.
Key Takeaways
Spring Boot uses Logback by default and reads configuration from logback-spring.xml.
Place logback-spring.xml in src/main/resources to customize logging behavior.
Define appenders, loggers, and root settings in the XML to control log output and levels.
Use logback-spring.xml (not logback.xml) to enable Spring Boot features like profile support.
Set appropriate log levels to see the logs you need without clutter.