Guide to Logback in Spring Boot
Overview
Logback is an excellent choice for application logging. It's easy and highly configurable and has a small memory footprint.
While many logging frameworks can be used with java, spring boot comes with logback as the default logging framework.
Using Logback with Spring Boot
Spring boot offers a starter-logging dependency with framework defaults. The only maven dependency we need is
For a web application, you need only spring-boot-starter-web since it fetches starter-logging transitively.
Spring boot logger starter comes with sensible defaults, which are provided using four XML configuration files placed under org/springframework/boot/logging/logback/
- defaults.xml - Provides conversion rules, pattern properties, and common logger configurations.
- console-appender.xml - Adds a ConsoleAppender using the CONSOLE_LOG_PATTERN.
- file-appender.xml - Adds a RollingFileAppender using the FILE_LOG_PATTERN with appropriate settings.
- base.xml - Include all the above configuration files and modify the root logger level to INFO.
Default log pattern
The output format printed on the appenders using the default setting looks like the below:
The above format contains below information:
- Date and Time: Millisecond precision and easily sortable.
- Log Level: ERROR, WARN, INFO, DEBUG, or TRACE.
- Process ID: Id of the JVM process.
- Separator: --- separator to distinguish the start of actual log messages.
- Thread name: Enclosed in square brackets.
- Logger name: This is usually the source class name (often abbreviated).
- The log message.
Default Log Behaviors
We will first verify default behavior with as source class and corresponding JUnit test class.
LoggingVerification.java - Prints different log statement
LoggingVerificationTest.java - Junit test to verify statement printed on the console.
Let's run the test case and observe the successful and failed test cases.
It's clear from the test result that WARN, INFO, and ERROR is passed and DEBUGand TRACE failed. It is because, by default, spring boot configures the root logger with level INFO.
Configuring and Customizing Loggers
Configure Logging Levels
We can configure the logging level using the property logging.level.<logger-name>=<level>, where the level is one of the values supported by the underlying logging framework. The root logger can be configured using logging.level.root.
Let's configure the root logger level to DEBUG and re-run our test cases.
Test result after adding the above property
This time DEBUG is passed.
Similarly, we can configure any logger in the application using different levels.
Configure Log File Path and Location
The log file location can be supplied using the property logging.file.name.
This will create a log file name scaler-logging.log in the specified path.
Configure Log Pattern
Log pattern can be configured using logging.pattern.<appender> property. For example, a custom console pattern can be provided using
The corresponding log on the console will be printed like below:
Logback Configuration through an External File
Logback configuration using the application.properties file works for many Spring Boot applications. However, large enterprise applications usually have far more complex logging requirements.
One such problem is structure of the log message. Log statements are configured and printed in plain text format, but this has one serious problem. Text data is unstructured, and it isn’t easy to analyze and query the log. JSON logging is to solve these problems, which can be easily used for data analysis and detecting patterns.
In a Spring Boot application, we can specify a Logback XML configuration file as logback.xml or logback-spring.xml in the project classpath.
We will now customize our application logging to JSON format using the external logback-spring.xml configuration file in the project classpath. Here is the code of logback-spring.xml.
The JSON log output corresponding to the above configuration looks like
This is much better than the plain text log; easily queriable and parsable.
Spring Boot Profiles in Logging
Spring Boot also addresses profile-specific logging by extending Spring profiles for Logback configuration with the SpringProfile element. Using this element in the logback-spring.xml file, we can include or exclude sections of logging configuration based on the active Spring profile.
For example, JSON logging is good for managed env; however, for local, we are good with plain text. Let's update our logback configuration to consider the spring profile.
In the above configuration file, we have configured two console appenders, stdout and jsonstdout. The logger's section uses the spring profile element to decide which appenders to use based on the active profile.
Conclusion
- Logback is the default framework that comes with spring boot.
- Spring boot configures logback with sensible defaults.
- Default logging options can be updated by setting logging.* properties in the application.properties
- Spring boot supports configuring logging using an external file logback-spring.xml on the classpath.
- Logging options can be customized based on spring profiles.