0
votes

I am trying to redirect Java Util Logging logs to Log4j 2.1 using the birdge provided log4j-jul-2.1, but stuck with classpath problems. I have all the log4j jars in the domains lib directory and used the following JVM parameter as mentioned in log4j documentation:

-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager

The problem is that the bridge LogManager provided by log4j is not available in Glassfish AppClassLoader during startup and it causes the exception below. I can fix that by modifying server main classpath (provided by -cp argument), but this is bad. Is there any other/better looking way to achieve that? Simply putting it into server main lib directory does not also work as its again not AppClassLoader.

Could not load Logmanager "org.apache.logging.log4j.jul.LogManager"
    java.lang.ClassNotFoundException: org.apache.logging.log4j.jul.LogManager
            at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
            at java.util.logging.LogManager$1.run(LogManager.java:167)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.util.logging.LogManager.<clinit>(LogManager.java:157)
            at java.util.logging.Logger.getLogger(Logger.java:287)
            at com.sun.enterprise.glassfish.bootstrap.ASMainHelper.<clinit>(ASMainHelper.java:67)
            at com.sun.enterprise.glassfish.bootstrap.ASMain.main(ASMain.java:54)
1

1 Answers

0
votes

I finally decided to use SLF4J and its SLF4JBridgeHandler for handling Java Util Logging calls. The main benefit of it is that it can be initialized at some later point when all the classes are loaded and after all required static initializations.

    SLF4JBridgeHandler.removeHandlersForRootLogger();
    SLF4JBridgeHandler.install();

I also switched to Logback as a backend as with this solution Log4j encounters performance problems for disable JUL loggers.