前言:关于Log4j2,SLF4J,LogBack以及其他的一些日志框架的区别,建议大家可以先了解一下
一、添加依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <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>
|
二、application.properties中添加配置
1 2
| # log4j2 配置文件地址,而且log4j2-spring.xml作为默认的配置文件名,在resources目录下是就算不配置也会默认读 logging.config=classpath:log4j2-spring.xml
|
三、创建相关配置文件
配置文件应该放在resources目录下,关于配置方面的详细内容,可以参考下面几篇博文以及项目demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| <?xml version="1.0" encoding="UTF-8"?>
<configuration status="info" monitorInterval="30"> <appenders> <console name="Console" target="SYSTEM_OUT"> <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> </console>
<RollingFile name="RollingFileDebug" fileName="${sys:user.home}/logs/hpaasvc/debug.log" filePattern="${sys:user.home}/logs/hpaasvc/$${date:yyyy-MM}/debug-%d{yyyy-MM-dd}-%i.log"> <Filters> <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/> <ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL"/> </Filters> <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> <Policies> <TimeBasedTriggeringPolicy/> <SizeBasedTriggeringPolicy size="100 MB"/> </Policies> </RollingFile>
<RollingFile name="RollingFileInfo" fileName="${sys:user.home}/logs/hpaasvc/info.log" filePattern="${sys:user.home}/logs/hpaasvc/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log"> <Filters> <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/> <ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/> </Filters> <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> <Policies> <TimeBasedTriggeringPolicy/> <SizeBasedTriggeringPolicy size="100 MB"/> </Policies> </RollingFile>
<RollingFile name="RollingFileWarn" fileName="${sys:user.home}/logs/hpaasvc/warn.log" filePattern="${sys:user.home}/logs/hpaasvc/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log"> <Filters> <ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/> <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/> </Filters> <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> <Policies> <TimeBasedTriggeringPolicy/> <SizeBasedTriggeringPolicy size="100 MB"/> </Policies> <DefaultRolloverStrategy max="20"/> </RollingFile>
<RollingFile name="RollingFileError" fileName="${sys:user.home}/logs/hpaasvc/error.log" filePattern="${sys:user.home}/logs/hpaasvc/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log"> <ThresholdFilter level="ERROR"/> <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> <Policies> <TimeBasedTriggeringPolicy/> <SizeBasedTriggeringPolicy size="100 MB"/> </Policies> </RollingFile>
</appenders> <loggers> <logger name="org.springframework" level="INFO"> </logger>
<root level="all"> <appender-ref ref="Console"/> <appender-ref ref="RollingFileDebug"/> <appender-ref ref="RollingFileInfo"/> <appender-ref ref="RollingFileWarn"/> <appender-ref ref="RollingFileError"/> </root> </loggers>
</configuration>
|
使用Log4j2进行日志记录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @RestController @RequestMapping("/users") public class UserController {
public final Logger logger= LoggerFactory.getLogger(getClass());
@Autowired UserService userService;
@PostMapping("/login") public RestResult login(@RequestBody User loginUser) { logger.debug("/users/login [Post] -> loginUser : {}", loginUser); return userService.login(loginUser); }
}
|
总结:在使用Log4j2后我个人感觉,他的优势和劣势都是使用xml进行配置的方便,为什么说这也是劣势呢,因为我看了好多才搞懂各个xml的参数含义等等,当然了,我觉得这不是Log4j2的劣势,是我的劣势 -_- 。