How to configure log4j log levels
The log level can be configured in the log4j.properties file as follows:
- Global log level configuration: Configure the global log level by setting the log4j.rootLogger property. For example, setting it to log4j.rootLogger=DEBUG means setting the global log level to DEBUG.
- Classifier log level configuration: You can control the logging output of a specific package or class by setting its log level using the log4j.logger property. For example, setting log4j.logger.com.example=INFO will set the log level of classes in the com.example package to INFO.
- Configuring the log output destination: You can set the log4j.appender property to configure the log output destination, such as console output or file output.
The content of the sample configuration file log4j.properties is as follows:
log4j.rootLogger=DEBUG, Console
# 控制台输出目标
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%-5p] %c - %m%n
# 设置特定包的日志级别
log4j.logger.com.example=INFO
The above configuration sets the log level to DEBUG and outputs logs to the console. For classes with the package name com.example, the log level is set to INFO.