本文介绍了Log4j JDK日志记录适配器:在启动过程中后期应用LogManager系统属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an WebSphere Application Server which runs an WebApp. I start the Server from Eclipse. The main logging framework in that application is log4j2, but there are some third party libraries which use java.util.logging. I want to redirect those logs to log4j2 so it uses my filters, log format etc. To achieve this I can use the Log4j JDK Logging Adapter.

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

Usually one would set the system property -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager, for example in the jvm.options. The problem is that I can't seem to make that solution work because the server uses java util logging long before my log4j2 properties are loaded and before the log4j-jul jar of the adapter is loaded. This causes an Exception to be thrown immediatly after trying to start the server (see my other question).

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

So I thought maybe it's possible to set the LogManager to the one from the log4j-jul jar later in the startup process, when the jar is and my log4j2 properties are loaded?

推荐答案

java.util.logging.LogManager 已加载(在第174行附近),它分配了单例实例.触发加载后无法替换它.

As soon as the java.util.logging.LogManager is loaded (near line 174) it assigns the singleton instance. There is no way to replace it after loading is triggered.

安装 org.apache.logging.log4j.jul.Log4jBridgeHandler 应该可以解决问题.您应该能够使用带有 WebSphere .

Installing the 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中来完成:

Usually this can be done by adding the following to your logging.properties:

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

每个 LOG4J2-2025 在LOG4J2版本3.0.0中已修复.截至2019年4月25日,这似乎是将来的版本.对于LOG4J2 2.3,您可以使用 log4j2-Java6-extras 从Maven下载log4j2-Java6-extras

Per 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.

这篇关于Log4j JDK日志记录适配器:在启动过程中后期应用LogManager系统属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 04:30