log4j升级为log4j2(不需要改动代码)
来源:互联网 发布:多益网络上市价格 编辑:程序博客网 时间:2024/05/18 00:00
http://blog.csdn.net/zouxucong/article/details/56013053
公司的项目决定升级log4j,因为log4j2有一个自动删除日志的功能,这样可以减轻运维的一些工作,而且在多线程环境下,log4j2的异步日志系统比log4j和logback提高了十倍的性能(吞吐量和延迟率),官方原文如下:
Log4j 2 contains next-generation Asynchronous Loggers based on the LMAX Disruptor library. In multi-threaded scenarios Asynchronous Loggers have 10 times higher throughput and orders of magnitude lower latency than Log4j 1.x and Logback.
因为是升级,原项目用的是log4j或者self4j打印日志,获取logger的方式不同
比如log4j:
import org.apache.log4j.Logger;
private static final Logger LOGGER = Logger.getLogger(xxxx.class);
比如self4j:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger logger = LoggerFactory.getLogger(xxxx.class);
而如果用log4j2的话,API将有所不同:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
private static final Logger logger = LogManager.getLogger(xxxx.class);
如果在升级的过程中,我们要去改代码的话,那将是一项很大的繁琐工程,log4j2架构已经考虑到这一点,给我们提供了转换的包,我们只需要导入几个转换包就可以使用log4j2,而且还不需要修改代码。
官方原文:
Perhaps the simplest way to convert to using Log4j 2 is to replace the log4j 1.x jar file with Log4j 2's log4j-1.2-api.jar. However, to use this successfully applications must meet the following requirements:
- They must not access methods and classes internal to the Log4j 1.x implementation such as Appenders, LoggerRepository or Category's callAppendersmethod.
- They must not programmatically configure Log4j.
- They must not configure by calling the classes DOMConfigurator or PropertyConfigurator
删掉pom.xml文件中关于log4j的依赖,添加新依赖
log4j-1.2-api为log4j和log4j2的连接包。
第二步:删掉以前的log4j.properties,改为log4j2.xml,这里起名字的时候,不要写错了
1.log4j2.*版本中获取home路径是${sys:catalina.base}
2.要在<root>标签中添加<appender-ref ref="Console"/>,<appender-ref ref="log"/>才能在控制台、和文件中打印日志
第三步:在web.xml中配置
注释掉以前关于加载log4j的配置,重新配置,这里我的log4j2.xml放在classpath/conf下
配置放在web.xml的<display-name/>下方如此log4j升级log4j2升级成功。
扩展:如果原项目中用的是self4j打印日志,那么导包的时候需要导入:
也是不需要修改代码,关于self4j继承log4j2的详细解决方案,可参考:http://blog.csdn.net/zouxucong/article/details/56005725- log4j升级为log4j2(不需要改动代码)
- log4j升级为log4j2(不需要改动代码)
- log4j升级为log4j2(不需要改动代码)
- log4j平滑升级为log4j2(不需改动源码)
- log4j-升级log4j2
- ArcGIS Web Appbuilder代码改动为不需要protal步骤初探
- log4j2 升级问题 (包含log4j与slf4j版本)
- log4j和log4j2
- log4j与log4j2
- Log4j1升级Log4j2实战
- log4j与log4j2的区别
- logback log4j log4j2 性能实测
- logback log4j log4j2 性能实测
- log4j 与 log4j2 的区别
- Log4j和Log4j2的区别
- Log4j和Log4j2的区别
- log4j 和log4j2配置差异
- 升级log4j2.x异步日志
- BeautifulSoup学习笔记1
- 同一元素上,通过v-on绑定多个相同事件的函数
- kotlin 的配置及使用
- boost库介绍
- Eclipse创建安卓虚拟机Intel X86报错!
- log4j升级为log4j2(不需要改动代码)
- 我的第一篇博客
- Centos6.5安装过程图解
- Linux LVM简明教程
- UE4 HTC VIVE
- 分布式系统:高性能系统设计原则
- Adb 那些事
- TLD的c++版本运行
- 理解PendingIntent以及在Notification中的使用