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

纵然是瞬间 提交于 2019-12-02 05:54:07

问题


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.

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

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?


回答1:


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.

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.

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

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.



来源:https://stackoverflow.com/questions/55832210/log4j-jdk-logging-adapter-apply-logmanager-system-property-late-in-the-startup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!