Learn to configure the logging levels for different packages in a Spring boot application using properties, YAML and XML configurations. We will see the different configurations are Logback and Log4j2 frameworks.
In the following examples, we can adjust the package names, class names and logging levels according to application-specific requirements. The logging levels can be one of TRACE
, DEBUG
, INFO
, WARN
, ERROR
, or FATAL
.
1. Configure Logging Levels using application.properties
The following is an example configuration for setting the logging levels using the /src/main/resources/application.properties
file.
# Console appender to display logs in the console
logging.level.root=INFO
# Logging level configurations for specific packages
logging.level.com.howtodoinjava=DEBUG
logging.level.org.springframework=INFO
# Logging level configurations for specific class
logging.level.com.howtodoinjava.MyClass=WARN
2. Configure Logging Levels using application.yaml
The following is an example configuration for setting the logging levels using the /src/main/resources/application.yaml
file.
# Console appender to display logs in the console
logging:
level:
root: INFO
# Logging level configurations for specific packages
com.howtodoinjava: DEBUG
org.springframework: INFO
# Logging level configurations for specific class
com.howtodoinjava.MyClass: WARN
3. Configure Logging Levels using Logback
The following is an example configuration for setting the logging levels when customizing it in the logback configuration files.
3.1. logback.properties
# Console appender to display logs in the console
appender.console.type=Console
appender.console.name=CONSOLE
appender.console.encoder.pattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n
# Root logger level
rootLogger.level=INFO
rootLogger.appenderRef.console.ref=CONSOLE
# Logging level configurations for specific packages
logger.com.howtodoinjava.name=com.howtodoinjava
logger.com.howtodoinjava.level=DEBUG
logger.org.springframework.name=org.springframework
logger.org.springframework.level=INFO
logger.com.howtodoinjava.MyClass.name=com.howtodoinjava.MyClass
logger.com.howtodoinjava.MyClass.level=WARN
3.2. logback.yaml
configuration:
appenders:
console:
name: CONSOLE
target: SYSTEM_OUT
encoder:
pattern: "%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n"
root:
level: INFO
appender-ref:
- ref: CONSOLE
loggers:
logger:
- name: com.howtodoinjava
level: DEBUG
- name: org.springframework
level: INFO
- name: com.howtodoinjava.MyClass
level: WARN
3.3. logback.xml
<configuration>
<!-- Console appender to display logs in the console -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- Root logger level -->
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
<!-- Logging level configurations for specific packages -->
<logger name="com.howtodoinjava" level="DEBUG" />
<logger name="org.springframework" level="INFO" />
<!-- Logging level configuration for specific class -->
<logger name="com.howtodoinjava.MyClass" level="WARN" />
<!-- Other logger configurations -->
</configuration>
4. Configuring Spring Boot Logging Levels for Log4j2
The following is an example configuration for setting the logging levels when customizing it in the Log4j2 configuration files.
4.1. log4j2.properties
# Console appender to display logs in the console
appender.console.type = Console
appender.console.name = CONSOLE
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n
# Root logger level
rootLogger.level = INFO
rootLogger.appenderRef.console.ref = CONSOLE
# Logging level configurations for specific packages
logger.com.howtodoinjava.name = com.howtodoinjava
logger.com.howtodoinjava.level = DEBUG
logger.org.springframework.name = org.springframework
logger.org.springframework.level = INFO
logger.com.howtodoinjava.MyClass.name = com.howtodoinjava.MyClass
logger.com.howtodoinjava.MyClass.level = WARN
4.2. log4j2.yaml
Configuration:
Appenders:
Console:
name: CONSOLE
target: SYSTEM_OUT
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n"
Loggers:
Root:
level: INFO
AppenderRef:
- ref: CONSOLE
Logger:
- name: com.howtodoinjava
level: DEBUG
- name: org.springframework
level: INFO
- name: com.howtodoinjava.MyClass
level: WARN
4.3. log4j2.xml
<Configuration>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="CONSOLE"/>
</Root>
<Logger name="com.howtodoinjava" level="DEBUG"/>
<Logger name="org.springframework" level="INFO"/>
<Logger name="com.howtodoinjava.MyClass" level="WARN"/>
</Loggers>
</Configuration>
5. Conclusion
In this Spring boot tutorial, we learned to configure logging levels using both properties and YAML/XML configurations for Logback and Log4j2 frameworks. We learned to customize the logging levels for specified classes and packages as well.
Happy Learning !!
Comments