loggingwebspherelog4j2websphere-libertyjava.util.logging

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


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?


Solution

  • 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 Oct 23, 2021, it appears that this issue was backported in LOG4J2-2025 jul-log4j bridgehandler into 2.15.0. For LOG4J2 2.3 you can use log4j2-Java6-extras or download log4j2-Java6-extras from Maven.