Logging in Spring Boot

1. Introduction

In this tutorial, we’ll discuss logging in Spring Boot. Spring Boot makes it very easy to implement logging in application. You can use all popular logging frameworks like Commons Logging, Log4J, or SLF4J with Spring Boot. Spring Boot makes it very easy to use logging frameworks and comes with default configurations. Generally, you’ll not need to change default but you can if you want to you can do without much effort

2. Default logging

  • For internal logging, Spring Boot uses Commons Logging.
  • Default configurations are provided for Logback, Java Util Logging and Log4J2.
  • Loggers are by default configured to use Console.
  • If you are using starter dependencies, Logback is by default used for logging.
  • By default, ERROR, WARN and INFO level messages are logged.

3. Log Levels

There are following levels:

  • INFO
  • WARN
  • OFF

Logger levels are configured in application.properties. The format of the property to set logging level is:


Here, level could be any value mentioned above.

The root logger can be configured by using logging.level.root.

Following is the example of configuring logger in application.properties:


If you are using application.yaml:

    org.springframework.web: "debug"
    org.hibernate: "error"

4. Log Format

The default log format is similar to the following:

2021-11-28 00:09:08.620  INFO 1988 --- [           main] c.l.l.LazyInitializationDemoApplication  : No active profile set, falling back to default profiles: default
2021-11-28 00:09:10.096  INFO 1988 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)

Lets analyze the log format.

2021-11-28 00:09:08.620This represents the date and time with millisecond precision.
INFOThis is the log level. The log level could be ERROR, WARN, INFO, DEBUG, or TRACE. Logback does not have FATAL level. FATAL level is same as ERROR.
1988This is the process ID.
– – –This is the separator
[ main]This is the thread name enclosed in square brackets.
c.l.l.LazyInitializationDemoApplicationThis is the logger name. It is usually the source class name.
No active profile set, falling back to default profiles: defaultThis is the log message.

5. Console output

By default, ERROR, WARN and INFO level messages are logged. But you can configure to log DEBUG and TRACE messages. You can set the DEBUG level to true in application.properties or use debug flag while starting application. With DEBUG mode, you see more information logged in by container, Hibernate and Spring Boot.

6. Colored output

You can set spring.output.ansi.enabled property to enable color output if your terminal supports ANSI. Following is the mapping of colors with log levels:



%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){red}

This will print messages in red color.

7. Custom Log configurations

Spring allows you to use different logging frameworks. To use a logging system with Spring, you have to include appropriate libraries in classpath. Different logging systems come with a configuration file which can be used to customize logging system. For example, Log4j2 expects log4j2.xml file. Configuration file should be in the root of the classpath. The configuration can also be specified using logging.config property.

Note: The property org.springframework.boot.logging.LoggingSystem is used to define custom logging system. The value is the fully qualified class name of a LoggingSystem implementation.

Following are the customization files for the logging system:

Logbacklogback-spring.xml, logback-spring.groovy, logback.xml, or logback.groovy
Log4j2log4j2-spring.xml or log4j2.xml
JDK (Java Util Logging)logging.properties

We’ll see examples of using these logging system with Spring Boot later.

8. Log messages to file

By default, Spring logs only to the console. However, you can log to a file as well in a specific directory. Following properties are used to configure log file and log file directory:

  • logging.file.name: The name of the log file. The log file could be absolute or relative to the current directory. For example, myApplication.log.
  • logging.file.path: The directory to which logs are written. The path of the directory could be absolute or relative to the current directory. For example, /var/log.

9. File rotation

Following are important points about file rotation:

  • Log files rotate when they reach 10 MB.
  • If you are using Logback in your application, then rotation can be configured using application.properties or application.yaml. For any other logging framework, you have to do it of your own.

Following are properties for rotation policy:

logging.logback.rollingpolicy.file-name-patternThe filename pattern to log archives.
logging.logback.rollingpolicy.clean-history-on-startTo set if archive cleanup should occur when the application starts.
logging.logback.rollingpolicy.max-file-sizeThe maximum size of log before it’s archived.
logging.logback.rollingpolicy.total-size-capThe maximum size of log archives after which it is deleted.
logging.logback.rollingpolicy.max-historyThe number of days for which the logs are retained. The default value is 7.

10. Log Groups

Sometimes you may want to group related loggers together. For example, you may want to configure all tomcat loggers together. This will help you to configure loggers easily.

Following is an example of how you can define a group tomcat in application.properties.


If you are using Yaml:

    tomcat: "org.apache.catalina,org.apache.coyote,org.apache.tomcat"

Once the log group is defined, log level can be defined like the following in application.properties:


And if you are using Yaml:

    tomcat: "trace"

Spring Boot includes web and sql pre-defined logging groups.

11. Log shutdown hook

A log shutdown hook is registered automatically when the application starts. The purpose of the log shutdown hook is to release logging resources when application terminates. You can disable this shutdown hook by using logging.register-shutdown-hook property:


12. Profile-specific Logging Configuration

You can use <springProfile> tag to optionally include or exclude sections of configuration based on active Spring profiles. The name attribute of this tag can have profile name or a profile expression as a value. This can be demonstrated with the help of following example:

<springProfile name="staging">
    <!-- configuration to be enabled when the "staging" profile is active -->

<springProfile name="dev | staging">
    <!-- configuration to be enabled when the "dev" or "staging" profiles are active -->

<springProfile name="!production">
    <!-- configuration to be enabled when the "production" profile is not active -->

13. Conclusion

In this tutorial, we touched concepts of logging in Spring Boot. This tutorial has taken reference from Spring Boot official documentation. We suggest you to read official documentation for further clarification and if you want to learn Spring Boot in depth.