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 uses Commons Logging for all its internal logging but leaves the underlying implementation open. When you use the Spring Boot starters, Logback is configured by default, with appropriate routing bridges so that libraries using Java Util Logging, Log4J, or SLF4J all feed into the same Logback pipeline. You do not need to change logging dependencies — the defaults work for most applications.

Default log format

The default output from Spring Boot looks like this:
2024-01-01T10:15:00.067+02:00  INFO 39599 --- [           main] o.example.Application : Starting Application
Each line includes:
  • Date and time — millisecond precision, ISO 8601 format
  • Log levelERROR, WARN, INFO, DEBUG, or TRACE
  • Process ID
  • --- separator — marks the start of the actual log message
  • Application name — in brackets, only if spring.application.name is set
  • Thread name — in brackets, may be truncated
  • Logger name — usually the source class name, often abbreviated
  • Log message
Logback does not have a FATAL level. Log4J2’s FATAL events are mapped to ERROR in Logback.

Setting log levels

Set log levels for any package or class in application.properties or application.yaml using logging.level.<logger-name>=<level>:
logging:
  level:
    root: "warn"
    org.springframework.web: "debug"
    org.hibernate: "error"
The root key configures the root logger, which acts as the default level for all loggers not explicitly configured. You can also set levels via environment variables. LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_WEB=DEBUG sets org.springframework.web to DEBUG. This technique works at the package level only — for class-level configuration, use SPRING_APPLICATION_JSON.

Debug and trace modes

Start with --debug to configure a selection of core loggers (embedded container, Hibernate, Spring Boot) at DEBUG level without changing your own code’s log level:
$ java -jar myapp.jar --debug
Use --trace for even more verbose output from the same core loggers:
$ java -jar myapp.jar --trace
debug=true or trace=true in application.properties has the same effect as the command-line flags.

Writing to a file

By default, Spring Boot logs only to the console. To also write to a file, set one of these properties:
logging:
  file:
    name: "app.log"
Or to write spring.log to a directory:
logging:
  file:
    path: "/var/log/myapp"
When both are set, logging.file.path is ignored and only logging.file.name is used. Log files rotate automatically when they reach 10 MB.

Logback rotation policy

Fine-tune Logback’s rolling policy directly from application.properties:
logging:
  logback:
    rollingpolicy:
      max-file-size: "10MB"
      max-history: 30
      total-size-cap: "1GB"
      clean-history-on-start: false

Log groups

Group related loggers together so you can configure them all with one setting:
logging:
  group:
    tomcat: "org.apache.catalina,org.apache.coyote,org.apache.tomcat"
  level:
    tomcat: "trace"
Spring Boot ships two pre-defined groups out of the box:
GroupLoggers
weborg.springframework.core.codec, org.springframework.http, org.springframework.web, org.springframework.boot.actuate.endpoint.web, org.springframework.boot.web.servlet.ServletContextInitializerBeans
sqlorg.springframework.jdbc.core, org.hibernate.SQL, org.jooq.tools.LoggerListener

Custom log configuration

Place a Logback or Log4j2 configuration file in the classpath root. Spring Boot picks it up automatically:
Logging systemFiles loaded
Logbacklogback-spring.xml, logback-spring.groovy, logback.xml, logback.groovy
Log4j2log4j2-spring.xml, log4j2.xml
Java Util Logginglogging.properties
Use the -spring variants (logback-spring.xml, log4j2-spring.xml) so Spring Boot can add profile-aware and environment-aware extensions such as <springProfile> and <springProperty> tags.

Logback environment properties

Inside logback-spring.xml, expose Spring Environment properties using the <springProperty> tag:
<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"
        defaultValue="localhost"/>
<appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">
    <remoteHost>${fluentHost}</remoteHost>
    ...
</appender>

Switching to Log4j2

To use Log4j2 instead of Logback, exclude the default Logback dependency and add the Log4j2 starter:
<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>
Do not include org.apache.logging.log4j:log4j-spring-boot in your build. Spring Boot ships its own Log4j2 extensions that supersede it.

Structured logging (Spring Boot 3.4+)

Structured logging writes log output in a machine-readable format — typically JSON — that log ingestion pipelines (Elasticsearch, Graylog, Logstash) can parse without custom regex patterns. Enable structured logging with a single property:
logging:
  structured:
    format:
      console: ecs     # or: gelf, logstash
      file: ecs
Spring Boot supports three formats out of the box:
logging:
  structured:
    format:
      console: ecs
    ecs:
      service:
        name: MyService
        version: "1.0"
        environment: Production
Example output:
{"@timestamp":"2024-01-01T10:15:00.067462556Z","log":{"level":"INFO","logger":"org.example.Application"},"process":{"pid":39599,"thread":{"name":"main"}},"service":{"name":"MyService"},"message":"Started Application"}

Customizing structured JSON output

Adjust field names and content using the logging.structured.json properties:
logging:
  structured:
    json:
      exclude: log.level
      rename:
        process.id: procid
      add:
        corpname: mycorp
For stack traces, control verbosity and format:
logging:
  structured:
    json:
      stacktrace:
        root: first
        max-length: 1024
        include-common-frames: true
        include-hashes: true

Startup info logging

Set spring.main.log-startup-info=false to suppress the startup banner and active-profile summary that Spring Boot prints at INFO level when the application starts.

Build docs developers (and LLMs) love