简体   繁体   中英

Redirect JUL to Log4J2 in Websphere WebApp

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.

I already asked questions regarding this topic here and here . First I tried to use the approach suggested by apache by setting the java util LogManager to the one provided by the log4j-jul jar. As seen in the first linked question this doesn't really work in my use case. Because the application runs as a WebApp in a Websphere Server, there isn't really a moment where the log4j-jul jar is loaded and I can swap out the LogManager , but before any calls to the java util Logger or LogManager are made because the Server uses those log statements before it loads the jar and configures my Log4J2 settings.

I then tried using the Log4jBridgeHandler as suggested in the second linked question. At first I thought that worked, as my test java util Log statements got printed with my currect Log4j2 format.

I do have two problems with that approach though: I can't use the way of setting the handler and level in logging.properties because it simply has no effect (I've set the path to the logging.properties file via the java.util.logging.config.file System Property in the jvm.options file).

What helped was using the install() method of the Log4jBridgeHandler , as in this made the java util logs be printed in my Log4j2 format. But The java util Logger I created did not have the required level of ALL but the default INFO , despite the root logger having the correct level. Because of this the default guard in the java util logging functions prevented the bridge from being entered so that I can never log anything below the INFO level.

So yeah I'm out of idead. Did I do anything wrong? Should what I've done have worked?

Did I do anything wrong? Should what I've done have worked?

The Log4jBridgeHandler does level propagation from LOG4J2 to JUL. Try adding a logger entry in your log4j2.xml to set the level of your named logger.

In your logging.properties you can turn this off via:

org.apache.logging.log4j.jul.Log4jBridgeHandler.propagateLevels=false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM