Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/spring-projects/spring-boot/llms.txt

Use this file to discover all available pages before exploring further.

Spring Boot auto-configures Logback as the default logging framework when spring-boot-starter-logging is on the classpath (pulled in transitively by most starters). Most logging changes can be made through application.properties or application.yaml without writing any XML — but when you need fine-grained control, Spring Boot provides clean extension points for custom Logback and Log4j2 configuration.

Set log levels

To control verbosity at runtime, set log levels using the logging.level prefix followed by the logger name:
logging:
  level:
    root: "warn"
    org.springframework.web: "debug"
    org.hibernate: "error"
    com.example: "info"
logging.level.root sets the global fallback level. Any package or class name can be used as the key. Valid levels are trace, debug, info, warn, error, fatal, and off.

Write logs to a file

By default, Spring Boot logs only to the console. To also write to a file, set logging.file.name:
logging:
  file:
    name: "myapplication.log"
To write to a specific directory (Spring Boot will name the file spring.log inside it), use logging.file.path instead:
logging:
  file:
    path: "/var/log/myapp"
Log files rotate at 10 MB by default. Set logging.logback.rollingpolicy.max-file-size to adjust this.

Customize the log format

To change the console log pattern:
logging:
  pattern:
    console: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
    file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"

Enable color output in the console

Spring Boot uses ANSI color output on terminals that support it. Color is enabled by default when a supported terminal is detected. To force-enable or force-disable it:
spring:
  output:
    ansi:
      enabled: always
Valid values for spring.output.ansi.enabled are always, detect (default), and never.
Color output is controlled by Spring Boot’s custom Logback converter. It appears in the console appender using the CONSOLE_LOG_PATTERN defined in org/springframework/boot/logging/logback/defaults.xml.

Use a custom logback-spring.xml

To apply Logback settings beyond what application.properties supports, add a logback-spring.xml to the root of your classpath (src/main/resources/logback-spring.xml). Using the -spring suffix lets you use Spring Boot’s Logback extensions (profile-based configuration, environment property injection). A typical custom configuration that reuses Spring Boot’s default patterns:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
    <root level="INFO">
        <appender-ref ref="CONSOLE" />
    </root>
    <logger name="org.springframework.web" level="DEBUG"/>
</configuration>
Spring Boot provides the following includable fragments under org/springframework/boot/logging/logback/:
FilePurpose
defaults.xmlConversion rules, pattern properties, common logger configs
console-appender.xmlConsole appender using CONSOLE_LOG_PATTERN
file-appender.xmlRolling file appender using FILE_LOG_PATTERN
structured-console-appender.xmlConsole appender with structured logging
structured-file-appender.xmlFile appender with structured logging

Configure file-only output

To disable console logging and write only to a file, import file-appender.xml but not console-appender.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration>
You also need to set the log file name in application.properties or application.yaml:
logging:
  file:
    name: "myapplication.log"
The following system properties are available inside Logback configuration and are set automatically by Spring Boot:
PropertyDescription
${PID}Current process ID
${LOG_FILE}Value of logging.file.name
${LOG_PATH}Value of logging.file.path
${LOG_EXCEPTION_CONVERSION_WORD}Value of logging.exception-conversion-word
${ROLLING_FILE_NAME_PATTERN}Value of logging.pattern.rolling-file-name

Switch from Logback to Log4j2

To replace Logback with Log4j2, exclude spring-boot-starter-logging and add spring-boot-starter-log4j2.
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</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>
To ensure that java.util.logging output is routed through Log4j2, set the system property java.util.logging.manager=org.apache.logging.log4j.jul.LogManager at JVM startup.

Use YAML or JSON configuration with Log4j2

Log4j2 supports YAML and JSON configuration files in addition to its default XML format. Add the required dependencies and name your file accordingly:
FormatAdditional dependencyFile name
YAMLjackson-databind + jackson-dataformat-yamllog4j2.yaml or log4j2.yml
JSONjackson-databindlog4j2.json or log4j2.jsn

Combine multiple Log4j2 configuration files

To merge a secondary configuration file on top of a primary one (useful for overriding specific loggers per environment), set logging.log4j2.config.override:
logging:
  log4j2:
    config:
      override: "optional:classpath:log4j2-override.xml"
The optional: prefix prevents the application from failing to start if the override file is absent.

Build docs developers (and LLMs) love