Springboot使用log4j2日志


1、pom.xml引用依赖

springboot默认是用logback的日志框架的,所以需要排除logback,不然会出现jar依赖冲突的报错

 1         
 2             org.springframework.boot
 3             spring-boot-starter-web
 4             
 5                 
 6                 
 7                     org.springframework.boot
 8                     spring-boot-starter-logging
 9                 
10             
11         
12         
13             org.springframework.boot
14             spring-boot-starter-log4j2
15         

2、配置文件yml文件和xml配置文件:两个文件均在resources下

application.yml部分配置如下

 1 mybatis-plus:
 2   mapper-locations: classpath:/mapper/**/*.xml 
 3   type-aliases-package: com.xxx.xxx.entity # 注意:对应实体类的路径
 4 
 5 
 6 
 7 
 8 logging:
 9   config: classpath:log4j2.xml
10   level:
11     com.xxx.xxx.dao: DEBUG #这个指定你的dao层,也就是mapper文件的所在包

日志的Level:

1、trace (追踪)
2、debug (调试)
3、info (信息)
4、warn (警告)
5、error (错误)
6、fatal (严重错误)

log4j2.xml配置如下:

  1 <?xml version="1.0" encoding="UTF-8"?>
  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         
 88             
 89         
 90         
 91             
 92         
 93 
 94         
 95         
 96         
 97         
 98         
 99 
100         
101             
102             
103             
104             
105         
106     
107 

参考:https://blog.csdn.net/u012150590/article/details/86601175

https://blog.csdn.net/whl190412/article/details/93487625

https://www.cnblogs.com/suizhikuo/p/15952005.html