[Play 2.5.0] Custom logging using Log4j2 :- ERROR Null object returned for AsyncLogger in Loggers

3,242 views
Skip to first unread message

Mukul Kumar

unread,
Apr 4, 2016, 9:58:08 PM4/4/16
to play-framework

Hi everyone, 
Following the link https://www.playframework.com/documentation/2.5.x/SettingsLogger, I am trying to configure custom logging to use log4j2 AsyncLogger. 
I want to use both sync and AsyncLogger (https://logging.apache.org/log4j/2.x/manual/async.html#Mixing_Synchronous_and_Asynchronous_Loggers). My log4j2.xml looks like:


<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<!-- Async Loggers will auto-flush in batches, so switch off immediateFlush. -->
<RandomAccessFile name="ASYNC" fileName="async.log" immediateFlush="false" append="false">
<PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C (%F:%L) - %X{requestId} %m%n"/>
</RandomAccessFile>
<RandomAccessFile name="SYNC" fileName="sync.log" immediateFlush="true" append="false">
<PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C (%F:%L) - %X{requestId} %m%n"/>
</RandomAccessFile>
</Appenders>

<Loggers>
<!-- AsyncLogger, pattern layout actually uses location, so we need to include it -->
<AsyncLogger name="com.foo" level="debug" includeLocation="true" additivity="false">
<AppenderRef ref="ASYNC"/>
</AsyncLogger>

<!-- root loggers -->
<Root level="debug" includeLocation="true">
<AppenderRef ref="SYNC"/>
</Root>
</Loggers>
</Configuration> 

I run my app  activator run -Dlog4j.configurationFile=conf/log4j2.xml and run into below error:

2016-04-04 18:35:48,026 pool-4-thread-5 ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.async.AsyncLoggerConfig for element AsyncLogger.
2016-04-04 18:35:48,029 pool-4-thread-5 ERROR Null object returned for AsyncLogger in Loggers.

After spending quite some time on it, I am starting to think if it's not supported in Play. So before creating an issue thought of checking if anyone else too ran into this or possible workaround.
Appreciate your assistance!

Thanks
-Mukul

Will Sargent

unread,
Apr 5, 2016, 12:06:20 AM4/5/16
to play-framework
Hi -- does https://github.com/wsargent/play-2.5-log4j2 work for you?

Will.

Mukul Kumar

unread,
Apr 5, 2016, 1:49:08 AM4/5/16
to play-framework
Hey Will, thanks for your reply and sharing the github app. 
I tried it already, it works great with only sync logger. When I configured AsyncLogger, I couldn't get it working.

P.S. I am using java version of Log4J2LoggerConfigurator. Though chances are rate, might that make a difference ?

Thanks

Will Sargent

unread,
Apr 5, 2016, 12:36:01 PM4/5/16
to play-fr...@googlegroups.com, Mukul Kumar
I’m sorry, I’m not clear.  I didn’t write a Java version of Log4J2LoggerConfigurator in that project. I don’t understand what’s not working for you. 

Will.
--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/c435d86f-ac46-4dbb-b119-944a2d09928f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mukul Kumar

unread,
Apr 5, 2016, 2:50:21 PM4/5/16
to play-framework, mukul....@gmail.com
Sorry Will if I wasn't clear before. What I meant was, I was able to get log4j2 working but the AsyncLogger is not working. 

This application configures custom logging  with Log4j2 AsyncLogger.
Run it using activator -Dlog4j.configurationFile=conf/log4j2.xml

Log4j2.xml is configured to use two loggers, root and an AsyncLogger. It will also created two log files sync.log and async.log
All the log stmts those use AsyncLogger should go inside async.log

When I run this application, I ran into below stack trace: 

--------------------------------------------------------------------------------------------------------------------
No play.logger.configurator found: logging must be configured entirely by the application.
--- (Running the application, auto-reloading is enabled) ---

2016-04-05 11:35:13,797 pool-4-thread-2 ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.async.AsyncLoggerConfig for element AsyncLogger.
2016-04-05 11:35:13,800 pool-4-thread-2 ERROR Null object returned for AsyncLogger in Loggers.
INFO  | 2016-04-05 11:35:14 | [pool-4-thread-2] play.api.LoggerLike$class (Logger.scala:92) -  Listening for HTTP on /0:0:0:0:0:0:0:0:9000

Server started, use Cmd+P to stop

DEBUG | 2016-04-05 11:35:15 | [ForkJoinPool-1-worker-1] play.api.LoggerLike$class (Logger.scala:73) -  Starting application default Akka system: application
INFO  | 2016-04-05 11:35:15 | [ForkJoinPool-1-worker-1] play.api.LoggerLike$class (Logger.scala:92) -  Application started (Dev)
--------------------------------------------------------------------------------------------------------------------

As I am using AsyncLogger, my debug logs should be going to async.log. But it's writing it to sync.log ( reason looks obvious from the stack trace above ERROR Null object returned for AsyncLogger in Loggers). I also verified this log4j configuration works just fine in a simple java application. 

Appreciate your assistance!
-Mukul


On Tuesday, 5 April 2016 09:36:01 UTC-7, Will Sargent wrote:
I’m sorry, I’m not clear.  I didn’t write a Java version of Log4J2LoggerConfigurator in that project. I don’t understand what’s not working for you. 

Will.

Mukul Kumar

unread,
Apr 6, 2016, 2:03:42 PM4/6/16
to play-framework
Okie, my bad. Upon further investigation I found I was missing the LMAX disrupter dependency in build.sbt, which caused AsyncLogger to be set to null. The error messages from console weren't good enough to figure this out. 

So, the fix is to add "com.lmax" % "disruptor" % "3.3.4", to build.sbt.

Thanks
Reply all
Reply to author
Forward
0 new messages