We know that the default logging tool in spring boot is logback, but for developers accustomed to log4j, spring boot can still be very well supported, just need to do some small configuration functions. Spring boot using log4j only takes a few steps
Introducing log4j Dependencies
When we created the Spring Boot project, we introduced it, spring-boot-starter
which contained the spring-boot-starter-logging
spring boot default log framework Logback, so before we introduce log4j, we need to rule out the dependencies of the package and introduce log4j dependencies. Just like this.
<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-log4j</artifactId> </dependency>
Configure Log4j.properties
With the introduction of log4j dependencies, you src/main/resources
log4j.properties
can begin configuring the application's logs by simply adding the configuration file to the directory.
Console output
With the following configuration, the output level of the root log is set to Info,appender to console output stdout
# log4j configuration log4j.rootcategory=INFO, stdout# console output log4j.appender.stdout= Org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout= Org.apache.log4j.PatternLayoutlog4j.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 problems, but to the production or testing environment, perhaps to persist the log content, easy to trace the cause of the problem. You can output to a different file by day by adding the following Appender content, and you need to add a log4j.rootCategory
appender named file so that the root log can be exported to logs/all.log
a file.
log4j.rootcategory=INFO, stdout,filelog4j.appender.file= Org.apache.log4j.DailyRollingFileAppenderlog4j.appender.file.file=logs/ All.loglog4j.appender.file.DatePattern='. 'yyyy-mm-ddlog4j.appender.file.layout= Org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%d{yyyy-mm-dd hh:mm:ss,sss}%5p% c{1}:%l-%m%n
Categorical output
When we have more log volume, it is very difficult to find the problem, the common means is to classify the log, such as: can be output according to different package. By defining the output to logs/my.log
the Appender, and setting the com.ysl
log level under the package to the debug level, the Appender is set to the output to logs/my.log
the named ysl
Appender
# COM.YSL Packet under the log configuration Log4j.category.com.ysl=DEBUG, didifile# com.ysl log output Log4j.appender.didifile= org.apache.log4j.DailyRollingFileAppenderlog4j.appender.didifile.file=logs/ My.loglog4j.appender.didifile.DatePattern='. 'yyyy-mm-ddlog4j.appender.didifile.layout= Org.apache.log4j.PatternLayoutlog4j.appender.didifile.layout.ConversionPattern=%d{yyyy-mm-dd Hh:mm:ss,sss} %5p%c{1}:%l----%m%n
- You can classify different levels, such as outputting the error level to a specific log file, which can be configured as follows.
log4j.logger.error=errorfile# error log output log4j.appender.errorfile= Org.apache.log4j.DailyRollingFileAppenderlog4j.appender.errorfile.file=logs/ Error.loglog4j.appender.errorfile.DatePattern='. 'yyyy-mm-= ERRORlog4j.appender.errorfile.layout= Org.apache.log4j.PatternLayoutlog4j.appender.errorfile.layout.ConversionPattern=%d{yyyy-mm-dd hh:mm:ss,sss }%5p%c{1}:%l-%m%n
Use log4j in Spring boot