This post explores diverse approaches to configuring logging levels within a Spring Boot application. Spring Boot allows for the customization of logging levels through various means such as application properties, YAML or YML files, command-line arguments, and XML files like logback.xml and log4j2.xml. The choice of which logs to display is contingent on the logging level configuration. By default, Spring Boot employs Apache Commons Logging for system logs. In scenarios requiring debugging and support, especially in production environments, effective management of logging levels becomes crucial. As the volume of printed logs increases, challenges arise in terms of log readability and debugging. Striking a balance is essential to avoid either inundating logs or missing critical entries crucial for flow analysis and debugging. Consequently, configuring the Spring Boot logging level is pivotal for achieving an optimal balance in the printed log volume.
Different Types of Logging Levels
The logging framework, such as Apache Commons Logging, provides a range of seven distinct logging levels, each serving a specific purpose. Among these levels, DEBUG and INFO are the two most commonly utilized. The following is a list of the logging levels in their order:
ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF
These levels offer a hierarchical structure, allowing developers to tailor the logging output based on the significance and granularity of the information they wish to capture.
When a specific logging level is chosen from the list mentioned above, the selected level and all lower levels, including higher levels, will be included in the log file. For instance, if the logging level is configured to ERROR, both ERROR and FATAL logs will be printed. Similarly, if the INFO logging level is selected, logs of types INFO, WARN, ERROR, and FATAL will be included in the output. This hierarchical behavior ensures that logs capture a comprehensive range of information, offering flexibility in managing and analyzing the application’s behavior.
1. application.properties
Configuring the logging level in a Spring Boot application is conveniently achieved through the application.properties
file. This file is typically located in the src/main/resources
folder and is created by default in a Spring Boot application. Utilize the key-value format within the application.properties
file to set various properties, including the Spring Boot logging level.
The forthcoming example will illustrate how to configure the logging level in a Spring Boot application using the key-value format within the application.properties
file.
src/main/resources/application.properties
logging.level.root=DEBUG
2. application.yml
An alternative method for configuring the Spring Boot logging level is by utilizing the YAML format. YAML, often humorously referred to as “YAML Ain’t Markup Language,” is a human-friendly data serialization language applicable to various programming languages. In a Spring Boot application, you can incorporate configuration settings in a YAML file.
To implement this approach, introduce an application.yml
file to the src/main/resources
folder of the Spring Boot application. The provided configuration snippet below demonstrates how to set the Spring Boot logging level using YAML:
src/main/resources/application.yml
logging: level: root: DEBUG
This YAML configuration specifies the logging level for the root logger as INFO. Adjust the value as needed based on the desired logging level for your application.
3. Command Line
The Spring Boot logging level can also be configured as a Java argument in the command line. After compiling and saving the Spring Boot application as a JAR file, you can set the logging level when invoking the JAR file using the java
command. Command line arguments are employed to initiate the Java application with the specified logging level.
The Java command for running the application with logging level configuration is exemplified below:
java -jar <jar> --debug java -jar YourSpringBootApp.jar --logging.level.root=INFO
In this example, replace YourSpringBootApp.jar
with the actual name of your JAR file. The --logging.level.root=INFO
argument sets the logging level for the root logger to INFO. Adjust the logging level as per your requirements.
4. Spring Boot profile logging
The Spring Boot application can be configured to run based on different profiles, enabling distinct setups for development, QA, integration testing, performance testing, and production environments. As each environment may require specific configurations, the Spring Boot profile feature provides a solution. The setup for Spring Boot profiles is demonstrated below:
src/main/resources/bootstrap.properties
spring.profiles.active=prod
src/main/resources/application-prod.properties
logging.level.root=DEBUG
5. logback.xml
The SLF4J logging framework is supported in Spring Boot applications. SLF4J utilizes the logging configuration specified in the logback.xml
file. This XML file is responsible for configuring logging properties. Prior to starting the Spring Boot application, SLF4J reads the logback.xml
file and configures the logging settings accordingly. Placing the logback.xml
file in the src/main/resources
folder allows SLF4J to automatically detect and apply its settings. A sample logback.xml
file configuration is provided below:
src/main/resources/logback.xml
<configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern> %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n </Pattern> </layout> </appender> <root level="DEBUG"> <appender-ref ref="CONSOLE"/> </root> </configuration>
This sample logback.xml
configuration sets up a console appender with a specific logging pattern and establishes the root logger level to DEBUG. Adjust the configuration based on your logging requirements.
6. log4j2.properties
Spring Boot provides support for the Log4j framework, specifically Log4j 2. Log4j 2 utilizes the log4j2.properties
file for configuring logging settings. However, it’s important to note that Spring Boot does not support Log4j 2 by default. To enable Log4j 2, you need to include the necessary dependencies in the pom.xml
file.
Firstly, remove the default Spring Boot starter logger from the pom.xml
file and replace it with the Spring Boot starter for Log4j 2. Once this is done, you can configure the log4j2.properties
file as illustrated below:
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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>
src/main/resources/log4j.properties
appenders = console appender.console.type = Console appender.console.name = STDOUT appender.console.layout.type = PatternLayout appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n rootLogger.level = info rootLogger.appenderRefs = stdout rootLogger.appenderRef.stdout.ref = STDOUT
In the log4j2.properties
configuration, this example sets up a console appender with a specific logging pattern and defines the root logger level as INFO. Adjust the configuration according to your logging requirements.
7. log4j2.xml
Certainly, configuring Log4j2 settings using an XML file is a common practice. When logging settings are specified in the log4j2.xml
file, the Spring Boot application will utilize Log4j2 to interpret and execute the loggers. Since Log4j2 is not supported by default in Spring Boot applications, it needs to be added as a dependency in the pom.xml
file. The following are the changes required in the pom.xml
file and the log4j2.xml
file:
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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>
src/main/resources/log4j.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="DEBUG" monitorInterval="30"> <Properties> <Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property> </Properties> <Appenders> <Console name="Console" target="SYSTEM_OUT" follow="true"> <PatternLayout pattern="${LOG_PATTERN}" /> </Console> </Appenders> <Loggers> <Logger name="com.yawin" additivity="false"> <AppenderRef ref="Console" /> </Logger> <Root level="info"> <AppenderRef ref="Console" /> </Root> </Loggers> </Configuration>
In this log4j2.xml
configuration, a console appender is defined with a specific logging pattern, and the root logger level is set to INFO. Adjust the configuration based on your logging preferences.