Java and Spring Level 1 This level · 78 of 105
Easy java spring

How does Spring Boot handle logging by default?

The 30 second answer

With the usual starters, Spring Boot includes a logging starter that selects Logback by default and bridges other logging APIs into the same backend. Spring Boot's own code uses Spring's Commons Logging bridge. Logging is initialized early, before the application context, so startup messages are captured too. By default, output goes to the console at INFO level. I can configure logger levels, enable file output or replace the backend rather than changing application logging calls.

Could you say this in an interview?

1. The default libraries. spring-boot-starter-logging is just:

dependencies {
    api("ch.qos.logback:logback-classic")
    api("org.apache.logging.log4j:log4j-to-slf4j")
    api("org.slf4j:jul-to-slf4j")
}

spring-boot-starter-logging/build.gradle. So library code written against Log4j 2 or JUL ends up in Logback.

2. Choosing the logging system.

public static LoggingSystem get(ClassLoader classLoader) {
    String loggingSystemClassName = System.getProperty(SYSTEM_PROPERTY);
    if (StringUtils.hasLength(loggingSystemClassName)) {
        if (NONE.equals(loggingSystemClassName)) {
            return new NoOpLoggingSystem();
        }
        return get(classLoader, loggingSystemClassName);
    }
    LoggingSystem loggingSystem = SYSTEM_FACTORY.getLoggingSystem(classLoader);
    Assert.state(loggingSystem != null, "No suitable logging system located");
    return loggingSystem;
}

LoggingSystem.java L150-L161. The factories are loaded from spring.factories and ordered: Logback has @Order(Ordered.HIGHEST_PRECEDENCE + 1024) (LogbackLoggingSystem.java L498), Log4j 2 has @Order(0) (Log4J2LoggingSystem.java L516), and JUL is last (JavaLoggingSystem.java L170). You can force one with the system property org.springframework.boot.logging.LoggingSystem.

3. When it is configured. Logging is initialized very early (on ApplicationStartingEvent) and configured once the Environment is ready, so logging.* values from application.yml apply.

4. Defaults and common properties.

logging:
  level:
    root: INFO
    org.hibernate.SQL: DEBUG
  file:
    name: logs/app.log
  structured:
    format:
      console: ecs
  • Console only by default; no file unless logging.file.name or logging.file.path is set.
  • Files rotate at 10 MB by default (Logback).
  • Log groups: logging.level.web=DEBUG and logging.level.sql=DEBUG are built-in groups.
  • Structured JSON logs (ecs, gelf, logstash) are built in since Spring Boot 3.4.

5. Custom config files. logback-spring.xml is preferred over logback.xml because Spring Boot processes it and supports <springProfile> and <springProperty>.

Switching to Log4j 2: exclude spring-boot-starter-logging and add spring-boot-starter-log4j2.

Sources (2)
  1. Spring Boot reference: Loggingdocs.spring.io
  2. Spring Boot how-to: Loggingdocs.spring.io
Previous
Next