🌱 Spring Boot Q19 / 69

How does Spring Boot handle logging?

AI-Powered Answer ✓ Answered

Spring Boot provides robust and flexible logging capabilities, offering sensible defaults out-of-the-box while also allowing extensive customization. It leverages standard logging facades and frameworks to provide a consistent logging experience for developers.

Default Logging Behavior

By default, Spring Boot uses Commons Logging for internal logging and auto-configures Logback as the logging implementation if it's available on the classpath (which it usually is, via spring-boot-starter-logging). If Logback isn't available, Spring Boot falls back to Java Util Logging, Log4j2, or even a simple logger.

The default configuration prints log messages to the console with INFO level and above. The output includes: timestamp, thread name, log level, logger name (usually the class name), and the message. It also provides color-coded output on most terminal environments for better readability.

Configuration via application.properties/application.yml

One of the easiest ways to configure logging in Spring Boot is through the application.properties or application.yml file. You can set the global log level, specific log levels for packages/classes, and configure file output.

Setting Log Levels

You can set the log level for different loggers (root, packages, or specific classes) using the logging.level prefix.

properties
logging.level.root=WARN
logging.level.org.springframework=INFO
logging.level.com.example.myapp=DEBUG

File Logging

To enable logging to a file in addition to the console, you can specify logging.file.name or logging.file.path. If logging.file.name is used, the file is created in the current directory. If logging.file.path is used, a spring.log file is created in that path.

properties
# Log to a specific file name
logging.file.name=my-application.log

# Or, log to a default 'spring.log' in a specified directory
logging.file.path=/var/log/myapp

# Configure file rotation and maximum size
logging.logback.rollingpolicy.max-file-size=10MB
logging.logback.rollingpolicy.max-history=7

Using Different Logging Frameworks

While Logback is the default, Spring Boot doesn't force you to use it. You can switch to Log4j2 or Java Util Logging by excluding Logback and including the desired framework's starter.

Switching to Log4j2

xml
<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>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

Custom Logging Configuration Files

For more advanced logging configurations (e.g., custom appenders, complex filters, different patterns), you can provide a framework-specific configuration file. Spring Boot will pick up files with specific names from the classpath, such as:

  • Logback: logback-spring.xml, logback-spring.groovy, logback.xml, logback.groovy
  • Log4j2: log4j2-spring.xml, log4j2.xml
  • Java Util Logging: logging.properties

Using the -spring variant (e.g., logback-spring.xml) allows you to use Spring Boot's advanced features, such as externalizing configuration and property substitution, within the logging config file itself.

xml
<!-- src/main/resources/logback-spring.xml -->
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="com.example.myapp" level="DEBUG"/>
    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
    </root>
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>${LOG_FILE}</file>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
</configuration>

Key Takeaways

  • Spring Boot uses Commons Logging and defaults to Logback for implementation.
  • Sensible defaults provide console output at INFO level.
  • Logging can be easily configured using application.properties/application.yml for log levels and file output.
  • Supports Logback, Log4j2, and Java Util Logging, allowing developers to switch frameworks.
  • Allows custom framework-specific configuration files (e.g., logback-spring.xml) for advanced setups.
  • Integration with spring-boot-starter-logging simplifies dependency management.