Payara micro to log with slf4j and log4j2

1,769 views
Skip to first unread message

Myron Tuang

unread,
Mar 22, 2017, 6:41:40 AM3/22/17
to Payara Forum
Hi,

Can i change Payara micro default logging to use with slf4j and log4j2?

Thanks,
~myron

Harry Chan

unread,
Mar 22, 2017, 10:04:21 AM3/22/17
to Payara Forum
Definitely yes, this is the example of using logback(change it to log4j2 yourself) with slf4j
<dependencies>
    <!-- logger -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logback.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
...
</dependencies>

Myron Tuang

unread,
Mar 22, 2017, 12:05:43 PM3/22/17
to Payara Forum
Hi,
I know that you can do that with payara server with the following instruction:

http://www.c2b2.co.uk/middleware-blog/alternative-logging-frameworks-for-application-servers-glassfish.php

My question is can you do similar configuration with payara micro.

Thanks,
~myron

Harry Chan

unread,
Mar 24, 2017, 11:59:22 AM3/24/17
to Payara Forum
Hi Myron,

I am sorry that I got you wrongly and I can't find any related document too.

However, I still could achieve it using my way, please check it on https://github.com/hei1233212000/payara-micro-log4j2
Thank you.

Best Regards,
Harry Chan

Myron Tuang

unread,
Mar 30, 2017, 12:17:54 PM3/30/17
to Payara Forum
Hi Harry,

1.  I downloaded from link you provided and build the package as shown the the jpeg with the target directory:


Run as suggested and it worked.

2.  I then removed unnecessary files and directories as shown in the jpeg with target1 directory leaving the libs directory intact:

Run as suggested and it worked!

3.  I then removed the libs directory under the payara-micro directory as shown in the jpeg with target2 directory:

It throws the following exceptions:

Can't load log handler "org.slf4j.bridge.SLF4JBridgeHandler"
java.lang.ClassNotFoundException: org.slf4j.bridge.SLF4JBridgeHandler
java.lang.ClassNotFoundException: org.slf4j.bridge.SLF4JBridgeHandler
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.util.logging.LogManager$5.run(LogManager.java:965)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.util.logging.LogManager.loadLoggerHandlers(LogManager.java:958)
        at java.util.logging.LogManager.initializeGlobalHandlers(LogManager.java:1578)
        at java.util.logging.LogManager.access$1500(LogManager.java:145)
        at java.util.logging.LogManager$RootLogger.accessCheckedHandlers(LogManager.java:1667)
        at java.util.logging.Logger.getHandlers(Logger.java:1777)
        at java.util.logging.Logger.log(Logger.java:735)
        at java.util.logging.Logger.doLog(Logger.java:765)
        at java.util.logging.Logger.log(Logger.java:788)
        at java.util.logging.Logger.info(Logger.java:1490)
        at fish.payara.micro.boot.runtime.PayaraMicroRuntimeBuilder.build(PayaraMicroRuntimeBuilder.java:63)
        at org.glassfish.embeddable.GlassFishRuntime._bootstrap(GlassFishRuntime.java:157)
        at org.glassfish.embeddable.GlassFishRuntime.bootstrap(GlassFishRuntime.java:110)
        at fish.payara.micro.impl.PayaraMicroImpl.bootStrap(PayaraMicroImpl.java:976)
        at fish.payara.micro.impl.PayaraMicroImpl.main(PayaraMicroImpl.java:201)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at fish.payara.micro.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at fish.payara.micro.boot.loader.Launcher.launch(Launcher.java:107)
        at fish.payara.micro.boot.loader.Launcher.launch(Launcher.java:70)
        at fish.payara.micro.boot.PayaraMicroLauncher.main(PayaraMicroLauncher.java:72)
        at fish.payara.micro.PayaraMicro.main(PayaraMicro.java:358)

4.  Next I moved the payara-micro.jar to the upper directory as shown in the jpeg with target3 directory:


It throws the same exceptions:

Can't load log handler "org.slf4j.bridge.SLF4JBridgeHandler"
java.lang.ClassNotFoundException: org.slf4j.bridge.SLF4JBridgeHandler
java.lang.ClassNotFoundException: org.slf4j.bridge.SLF4JBridgeHandler
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.util.logging.LogManager$5.run(LogManager.java:965)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.util.logging.LogManager.loadLoggerHandlers(LogManager.java:958)
        at java.util.logging.LogManager.initializeGlobalHandlers(LogManager.java:1578)
        at java.util.logging.LogManager.access$1500(LogManager.java:145)
        at java.util.logging.LogManager$RootLogger.accessCheckedHandlers(LogManager.java:1667)
        at java.util.logging.Logger.getHandlers(Logger.java:1777)
        at java.util.logging.Logger.log(Logger.java:735)
        at java.util.logging.Logger.doLog(Logger.java:765)
        at java.util.logging.Logger.log(Logger.java:788)
        at java.util.logging.Logger.info(Logger.java:1490)
        at fish.payara.micro.boot.runtime.PayaraMicroRuntimeBuilder.build(PayaraMicroRuntimeBuilder.java:63)
        at org.glassfish.embeddable.GlassFishRuntime._bootstrap(GlassFishRuntime.java:157)
        at org.glassfish.embeddable.GlassFishRuntime.bootstrap(GlassFishRuntime.java:110)
        at fish.payara.micro.impl.PayaraMicroImpl.bootStrap(PayaraMicroImpl.java:976)
        at fish.payara.micro.impl.PayaraMicroImpl.main(PayaraMicroImpl.java:201)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at fish.payara.micro.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at fish.payara.micro.boot.loader.Launcher.launch(Launcher.java:107)
        at fish.payara.micro.boot.loader.Launcher.launch(Launcher.java:70)
        at fish.payara.micro.boot.PayaraMicroLauncher.main(PayaraMicroLauncher.java:72)
        at fish.payara.micro.PayaraMicro.main(PayaraMicro.java:358)

5.  Next I replaced the built payara-micro.jar with the original payara-micro-4.1.1.171.0.1.jar as shown in the jpeg with target4 directory with the jars in the libs directory:


I then replaced the MANIFEST.MF with additional entry of

Class-Path: libs/jul-to-slf4j.jar libs/log4j-api.jar libs/log4j-core.j
 ar libs/log4j-slf4j-impl.jar libs/slf4j-api.jar

in it.

Run as suggested and it worked!

My point is that java didn't find the classes in the jars under the libs directory in your payara-micro.jar, rather it finds the classes in the jars in the libs directory in the system directory structure.

Thanks,
Myron

Steve Millidge

unread,
Mar 30, 2017, 12:33:21 PM3/30/17
to Payara Forum
Any libraries should be packaged into the MICRO-INF/lib directory in the Payara Micro jar not /lib.

Myron Tuang

unread,
Mar 31, 2017, 5:12:22 AM3/31/17
to Payara Forum
Hi Steve,

I put the required jars in the MICRO-INF/lib directory as suggested as shown in the jjpeg with target4 directory:


I tried with and without the following entry in the MANIFEST.MF, i get the same exceptions.

Class-Path: lib/jul-to-slf4j.jar lib/log4j-api.jar lib/log4j-core.j
 ar lib/log4j-slf4j-impl.jar lib/slf4j-api.jar

Thanks,
Myron
Reply all
Reply to author
Forward
0 new messages