混合log4j 1.x和log4j 2 [英] Mixing log4j 1.x and log4j 2
问题描述
我有一个使用log4j2编写的新应用程序,以利用它的一些新功能。它使用的其中一个库有点旧,用log4j 1.x构建。我遇到的问题是应用程序中的新代码写入日志,但旧库中的代码不会写入日志。我甚至有一个类是旧库中的某个子类,并且也被旧库中的代码调用,并且此代码也不会写入日志。我已将应用程序配置为使用log4j2版本2.0.2,并且在将旧库作为maven依赖项引用时明确排除了log4j。
I have a new application that is being written using log4j2, to take advantage of some of its new features. One of the libraries it uses is a bit older and was built with log4j 1.x. I'm having a problem where the new code in the application writes to the log, but the code in the old library does not write to the log. I even have one class which is a subclass of something in the old library and is also called by code in the old library, and this code also will not write to the log. I have configured the application to use log4j2 version 2.0.2, and I explicitly excluded log4j when referencing the old library as a maven dependency.
如何获取代码旧库登录到与我的新代码相同的文件?
How can I get the code in the old library logging into the same file as my new code?
重写旧库应该被认为是最后的选择
推荐答案
您是否知道log4j2包含log4j-1.2的桥接?您可以通过删除旧的log4j-1.2.17.jar来使用它,并包含这三个jar:
Are you aware that log4j2 includes a bridge for log4j-1.2? You can use it by removing the old log4j-1.2.17.jar, and include these three jars:
- log4j-api-2。 x.jar
- log4j-core-2.x.jar
- log4j-1.2-api-2.x.jar
这将导致应用程序对log4-1.2 API的所有调用都被路由到log4j2实现。常见问题解答有一个图表,可以澄清事情。 (别忘了从类路径中删除旧的log4j-1.2 jar!)
This will result in all calls that your application makes to the log4-1.2 API to be routed to the log4j2 implementation. The FAQ has a diagram that may clarify things. (Don't forget to remove the old log4j-1.2 jar from the classpath!)
这篇关于混合log4j 1.x和log4j 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!