Introduce log4j dependencies
When creating the Spring Boot project, we introduced spring-boot-starter
, which includes spring-boot-starter-logging
, the dependency content is Logback, the default logging framework of Spring Boot, so before we introduce log4j, we need to exclude the dependency of this package, and then introduce the dependency of log4j, like Here's what it looks like:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-log4j</artifactId>
</dependency>
Configure log4j.properties
After the log4j dependency is introduced, you only need src/main/resources
to add log4j.properties
a configuration file to the directory to start configuring and using the application's logs.
console output
Through the following configuration, set the output level of the root log to INFO, and the appender to output stdout to the console
# LOG4J配置
log4j.rootCategory=INFO, stdout
# 控制台输出
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
output to file
In the development environment, we just output to the console without a problem, but in the production or test environment, the log content may be persisted to facilitate tracing the cause of the problem. You can output to different files by day by adding the following appender content, and you also need to log4j.rootCategory
add an appender named file, so that the root log can be output to the logs/all.log
file.
#
log4j.rootCategory=INFO, stdout, file
# root日志输出
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.file=logs/all.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
Classification output
When we have a large amount of logs, it will be very difficult to find the problem. The common method is to classify the logs, such as:
- You can output by different packages. By defining the output to
logs/my.log
the appender, andcom.didispace
setting the log level under the package to the DEBUG level, the appender is set to the output tologs/my.log
the nameddidifile
appender.# com.didispace包下的日志配置 log4j.category.com.didispace=DEBUG, didifile # com.didispace下的日志输出 log4j.appender.didifile=org.apache.log4j.DailyRollingFileAppender log4j.appender.didifile.file=logs/my.log log4j.appender.didifile.DatePattern='.'yyyy-MM-dd log4j.appender.didifile.layout=org.apache.log4j.PatternLayout log4j.appender.didifile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L ---- %m%n
- Different levels can be classified, such as outputting the ERROR level to a specific log file. The specific configuration can be as follows.
log4j.logger.error=errorfile # error日志输出 log4j.appender.errorfile=org.apache.log4j.DailyRollingFileAppender log4j.appender.errorfile.file=logs/error.log log4j.appender.errorfile.DatePattern='.'yyyy-MM-dd log4j.appender.errorfile.Threshold = ERROR log4j.appender.errorfile.layout=org.apache.log4j.PatternLayout log4j.appender.errorfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
This article mainly introduces how to introduce log4j in spring boot, as well as some basic usage.