使用单个 log4j2 xml 文件配置 log4j2 和 log4j [英] Configuring log4j2 and log4j using a single log4j2 xml file

查看:80
本文介绍了使用单个 log4j2 xml 文件配置 log4j2 和 log4j的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将我的应用程序迁移到 log4j 2,并通过 log4j2.xml 对其进行了配置

I've migrated my application to log4j 2, and I've configured it via log4j2.xml

但是,我使用的一些库依赖于 log4j 1. 如果我使用以下命令运行应用程序:

However, some of the libraries I'm using depend on log4j 1. If I run the application using:

-Dlog4j.configurationFile=path/to/log4j2.xml

log4j 1 抱怨找不到配置文件.我正在使用 log4j 2 提供的 log4j 1.x 桥,log4j-1.2-api-2.0-rc1.jar.是否可以使用单个 log4j2.xml 进行配置?

log4j 1 complains about not finding a configuration file. I'm using the log4j 1.x bridge provided by log4j 2, log4j-1.2-api-2.0-rc1.jar. Is it possible to configure both using a single log4j2.xml?

我尝试过的另一种方法是同时配置 log4j 和 log4j2:

An alternative I've tried is configuring both log4j and log4j2 together:

-Dlog4j.configurationFile=path/to/log4j2.xml -Dlog4j.configuration=path/to/log4j.xml

我担心的是日志配置文件和输出的碎片.我还担心 log4j.xml 和 log4j2.xml 之间可能存在的冲突.例如日志文件 error.log 配置为在 log4j 1 中使用 FileAppender,在 log4j 2 中使用 RollingFileAppender.

My concern is fragmentation of my logging configuration files and output. I'm also concerned about possible conflicts between log4j.xml and log4j2.xml. e.g. the logfile error.log is configured to use a FileAppender in log4j 1 and a RollingFileAppender in log4j 2.

有什么建议吗?

[注意]

这是我看到的错误:

log4j:WARN No appenders could be found for logger (org.apache.activemq.util.ThreadPoolUtils).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

我使用的 log4j 2 版本是 log4j 2.0 rc1.

The version of log4j 2 I'm using is log4j 2.0 rc1.

[答案]

似乎 activemq-5.8.0.jar 与 log4j 1 捆绑在一起.解决方案只是在 activemq 之前加载 log4j 1.x 桥.

Seems like activemq-5.8.0.jar was bundled with log4j 1. The solution was simply to load the log4j 1.x bridge before activemq.

推荐答案

我建议使用包含在 log4j2 发行版中的 log4j-1.2 适配器.这样,任何编码到 log4j-1.2 API 的库都可以与 log4j2 一起使用,无需任何代码更改.

I would recommend using the log4j-1.2 adapter that is included in the log4j2 distribution. That way, any libraries coded to the log4j-1.2 API will work with log4j2 without any code changes.

您的类路径应包括:

  • log4j-api-2.6.1.jar
  • log4j-core-2.6.1.jar
  • log4j-1.2-api-2.6.1.jar
  • log4j2.xml

您的类路径应该包括:

  • log4j-1.2.x.jar
  • log4j.properties 或 log4j.xml(无论如何这些都会被 log4j2 忽略)

另见 http://logging.apache.org/log4j/2.x/faq.html#which_jars

这篇关于使用单个 log4j2 xml 文件配置 log4j2 和 log4j的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆