java 混合使用 log4j 1.x 和 log4j 2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26005149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mixing log4j 1.x and log4j 2
提问by FrustratedWithFormsDesigner
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.
我有一个使用 log4j2 编写的新应用程序,以利用它的一些新功能。它使用的库之一有点旧,是用 log4j 1.x 构建的。我遇到了一个问题,应用程序中的新代码写入日志,但旧库中的代码没有写入日志。我什至有一个类,它是旧库中某物的子类,也被旧库中的代码调用,并且此代码也不会写入日志。我已将应用程序配置为使用 log4j2 版本 2.0.2,并且在将旧库作为 maven 依赖项引用时,我明确排除了 log4j。
How can I get the code in the old library logging into the same file as my new code?
如何让旧库中的代码登录到与新代码相同的文件中?
Re-writing the old library should be thought of as a last-resort option
重写旧库应该被认为是最后的选择
回答by Remko Popma
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:
您是否知道 log4j2 包含 log4j-1.2 的桥接器?您可以通过删除旧的 log4j-1.2.17.jar 来使用它,并包含这三个 jar:
- log4j-api-2.x.jar
- log4j-core-2.x.jar
- log4j-1.2-api-2.x.jar
- log4j-api-2.x.jar
- log4j-core-2.x.jar
- log4j-1.2-api-2.x.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 diagramthat may clarify things. (Don't forget to remove the old log4j-1.2 jar from the classpath!)
这将导致您的应用程序对 log4-1.2 API 进行的所有调用都路由到 log4j2 实现。常见问题解答有一个图表,可以澄清事情。(不要忘记从类路径中删除旧的 log4j-1.2 jar!)