Log4j JDK 日志记录适配器:在启动过程后期应用 LogManager 系统 属性

Log4j JDK Logging adapter: Apply LogManager system property late in the startup process

我有一个运行 WebApp 的 WebSphere Application Server。我从 Eclipse 启动服务器。该应用程序中的主要日志记录框架是 log4j2,但也有一些使用 java.util.logging 的第三方库。我想将这些日志重定向到 log4j2,以便它使用我的过滤器、日志格式等。为此,我可以使用 Log4j JDK Logging Adapter.

通常会设置系统属性 -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager,例如在jvm.options。问题是我似乎无法使该解决方案起作用,因为服务器在加载我的 log4j2 属性之前和加载适配器的 log4j-jul jar 之前很久就使用了 java util 日志记录。这会导致在尝试启动服务器后立即抛出异常(请参阅我的 other question)。

所以我想也许可以在启动过程中稍后将 LogManager 设置为 log4j-jul jar 中的那个,当 jar 和我的 log4j2 属性加载时?

一旦 java.util.logging.LogManager 被加载(靠近第 174 行),它就会分配单例实例。触发加载后无法替换

正在安装 org.apache.logging.log4j.jul.Log4jBridgeHandler should do the trick. You should be able to add that handler to the root logger using the default LogManager with WebSphere

通常这可以通过将以下内容添加到您的 logging.properties:

.handlers=org.apache.logging.log4j.jul.Log4jBridgeHandler
.level.INFO
org.apache.logging.log4j.jul.Log4jBridgeHandler.level=ALL

根据 LOG4J2-2025 this is fixed in LOG4J2 version 3.0.0. As of 25-APR-2019, it appears that is a future release. For LOG4J2 2.3 you can use log4j2-Java6-extras or download log4j2-Java6-extras from Maven.