使用Slf4j集成Log4j2构建项目日志系统的完美解决方案
发布日期:2021-09-08 15:09:25 浏览次数:6 分类:技术文章

本文共 5936 字,大约阅读时间需要 19 分钟。

一、背景

  最近因为公司项目性能需要,我们考虑把以前基于的log4j的日志系统重构成基于Slf4j和log4j2的日志系统,因为,使用slf4j可以很好的保证我们的日志系统具有良好的兼容性,兼容当前常见几种日志系统,而使用log4j2而不是log4j是因为Log4j 1.x 在高并发情况下出现死锁导致cpu使用率异常飙升,而Log4j2.0基于LMAX Disruptor的异步日志在多线程环境下性能会远远优于Log4j 1.x和logback(官方数据是10倍以上)。

  关于slf4j的原理以及优点,请参见我的另一篇博客:

二、搭建步骤

  1.依赖管理

    1).删除项目中存在的Log4j1.x所必须的log4j和slf4j-log4j12等依赖。

      可以到项目的根目录,执行:mvn dependency:tree > tree.log,之后使用 cat tree.log | grep log4j命令进行查找。

1     
2
3
org.slf4j
4
slf4j-log4j12
5
6
7
log4j
8
log4j
9
10

    2).添加以下slf4j和log4j2的依赖.

1     
2
3
org.slf4j
4
slf4j-api
5
1.7.13
6
7
8
org.slf4j
9
jcl-over-slf4j
10
1.7.13
11
runtime
12
13 14
15
16
org.apache.logging.log4j
17
log4j-api
18
2.4.1
19
20
21
org.apache.logging.log4j
22
log4j-core
23
2.4.1
24
25
26
27
org.apache.logging.log4j
28
log4j-slf4j-impl
29
2.4.1
30
31
32
33
org.apache.logging.log4j
34
log4j-web
35
2.4.1
36
runtime
37
38 39
40
41
com.lmax
42
disruptor
43
3.2.0
44

  2.web.xml中设置log4j2的监听器和过滤器(servlet3.0及以上版本不需要该步操作)

1 
2
3
org.apache.logging.log4j.web.Log4jServletContextListener
4
5
6
log4jServletFilter
7
org.apache.logging.log4j.web.Log4jServletFilter
8
9
10
log4jServletFilter
11
/*
12
REQUEST
13
FORWARD
14
INCLUDE
15
ERROR
16

  注意:log4j2不再支持properties文件了,只支持xml,json或是yaml,不指定位置的情况下默认在src/main/resources下查找。

     如果需要自定义位置,需要在上面的web.xml中添加以下代码

1 
2
log4jConfiguration
3
/WEB-INF/classes/log4j2.xml
4

  3.log4j2.xml

1 
2 3
4 5
6
/opt/logs/hafiz/log4j2Demo/logs
7
error
8
9 10 11
12
13
14
15 16
19
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

  4.测试类UserController.java

1 package com.hafiz.www.controller; 2  3 import com.hafiz.www.po.UserEntity; 4 import com.hafiz.www.service.UserService; 5 import org.slf4j.Logger; 6 import org.slf4j.LoggerFactory; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.stereotype.Controller; 9 import org.springframework.web.bind.annotation.RequestMapping;10 import org.springframework.web.bind.annotation.RequestMethod;11 import org.springframework.web.bind.annotation.ResponseBody;12 13 import java.util.ArrayList;14 import java.util.List;15 16 /**17  * Desc:用户信息控制器18  * Created by hafiz.zhang on 2016/8/27.19  */20 @Controller21 @RequestMapping("/user")22 public class UserController {23 24     private static final Logger logger = LoggerFactory.getLogger(UserController.class);25 26     @Autowired27     private UserService userService;28 29     @RequestMapping(value = "/all", method = RequestMethod.GET)30     @ResponseBody31     public List
getAllUsers(){32 logger.info("[GET] /user/all getAllUsers");33 List
list = userService.getAllUsers();34 logger.debug("This is log of level of debug");35 logger.trace("log4j2 Demo");36 logger.error("哎呀,出错啦~");37 return list;38 }39 }

注意:在JVM启动参数中增加 -DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector 开启异步日志.

三、小问题:

  项目不打印日志并且报:SLF4J: Class path contains multiple SLF4J bindings.主要问题在于在项目中既存在slf4j-log4j12的jar包又存在log4j-slf4j-impl的jar包,导致出现了两个打印日志实现类,所以slf4j门面不知道具体应该使用哪一个进行输出日志,所以导致不打印日志

  因为我之前是log4j 1.x,并且我在web.xml里面指定了log4j.properties文件的位置,第一次运行项目,忘了修改其值为log4j2.xml,出现了"Context [] startup failed due to previous errors"的错误,导致项目启动不起来,web.xml中修改为如下方式以后就正常了。

1 
2
log4jConfiguration
3
/WEB-INF/classes/log4j2.xml
4

转载地址:https://blog.csdn.net/weixin_34326429/article/details/90195364 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:12.6. FAQ 常见问题
下一篇:[LeetCode] Path Sum III 二叉树的路径和之三

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月10日 12时10分42秒