How to use log4j2 in 2.5.x Java?

1,452 views
Skip to first unread message

duluo...@gmail.com

unread,
Mar 27, 2016, 2:17:22 AM3/27/16
to play-framework
I set ".enablePlugins(PlayJava).disablePlugins(PlayLogback)" and
  "org.apache.logging.log4j" % "log4j-slf4j-impl" % log4j2Version,
  "org.apache.logging.log4j" % "log4j-api" % log4j2Version,
  "org.apache.logging.log4j" % "log4j-core" % log4j2Version

then run with " -Dlog4j.configurationFile=conf/log4j2.xml ".

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

2016-03-27 14:16:01.671 INFO  play.core.server.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

Server started, use Alt+D to stop
2016-03-27 14:16:02.916 WARN  application - Logger configuration in conf files is deprecated and has no effect. Use a logback configuration file instead.
Mar 27, 2016 2:16:03 PM org.mongodb.morphia.logging.MorphiaLoggerFactory chooseLoggerFactory
INFO: LoggerImplFactory set to org.mongodb.morphia.logging.jdk.JDKLoggerFactory
2016-03-27 14:16:04.449 INFO  akka.event.slf4j.Slf4jLogger - Slf4jLogger started
2016-03-27 14:16:04.505 INFO  Remoting - Starting remoting
2016-03-27 14:16:04.610 INFO  Remoting - Remoting started; listening on addresses :[akka.tcp://appli...@192.168.0.152:2551]
2016-03-27 14:16:04.612 INFO  Remoting - Remoting now listens on addresses: [akka.tcp://appli...@192.168.0.152:2551]

Will Sargent

unread,
Mar 27, 2016, 3:51:49 PM3/27/16
to play-fr...@googlegroups.com
Please take a look at https://www.playframework.com/documentation/2.5.x/SettingsLogger#Using-a-Custom-Logging-Framework

You need to add a logger-configurator.properties file as a resource:

play.logger.configurator=Log4J2LoggerConfigurator

and define the Log4J2LoggerConfigurator class as described in the documentation.

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/0078d712-bf40-4f36-a7d7-dd50d8458ed1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

duluo...@gmail.com

unread,
Mar 29, 2016, 1:28:49 AM3/29/16
to play-framework
I define the Log4J2LoggerConfigurator class as described in the documentation.

But it doesn't work, and I get an error: 

Play cannot load "configs.Log4J2LoggerConfigurator". Please make sure you have logback (or another module
that implements play.api.LoggerConfigurator) in your classpath.
             
java.lang.ClassNotFoundException: configs.Log4J2LoggerConfigurator
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at play.api.LoggerConfigurator$.apply(LoggerConfigurator.scala:45)
    at play.api.LoggerConfigurator$$anonfun$apply$1.apply(LoggerConfigurator.scala:39)
    at play.api.LoggerConfigurator$$anonfun$apply$1.apply(LoggerConfigurator.scala:38)
    at scala.Option.flatMap(Option.scala:171)
    at play.api.LoggerConfigurator$.apply(LoggerConfigurator.scala:38)
    at play.api.inject.guice.GuiceApplicationBuilder.applicationModule(GuiceApplicationBuilder.scala:101)
    at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:181)
    at play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:123)
    at play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21)

I missed something else?

On Monday, March 28, 2016 at 3:51:49 AM UTC+8, Will Sargent wrote:
Please take a look at https://www.playframework.com/documentation/2.5.x/SettingsLogger#Using-a-Custom-Logging-Framework

You need to add a logger-configurator.properties file as a resource:

play.logger.configurator=Log4J2LoggerConfigurator

and define the Log4J2LoggerConfigurator class as described in the documentation.

Will.
On Sat, Mar 26, 2016 at 11:17 PM, <duluo...@gmail.com> wrote:
I set ".enablePlugins(PlayJava).disablePlugins(PlayLogback)" and
  "org.apache.logging.log4j" % "log4j-slf4j-impl" % log4j2Version,
  "org.apache.logging.log4j" % "log4j-api" % log4j2Version,
  "org.apache.logging.log4j" % "log4j-core" % log4j2Version

then run with " -Dlog4j.configurationFile=conf/log4j2.xml ".

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

2016-03-27 14:16:01.671 INFO  play.core.server.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

Server started, use Alt+D to stop
2016-03-27 14:16:02.916 WARN  application - Logger configuration in conf files is deprecated and has no effect. Use a logback configuration file instead.
Mar 27, 2016 2:16:03 PM org.mongodb.morphia.logging.MorphiaLoggerFactory chooseLoggerFactory
INFO: LoggerImplFactory set to org.mongodb.morphia.logging.jdk.JDKLoggerFactory
2016-03-27 14:16:04.449 INFO  akka.event.slf4j.Slf4jLogger - Slf4jLogger started
2016-03-27 14:16:04.505 INFO  Remoting - Starting remoting
2016-03-27 14:16:04.610 INFO  Remoting - Remoting started; listening on addresses :[akka.tcp://application@192.168.0.152:2551]
2016-03-27 14:16:04.612 INFO  Remoting - Remoting now listens on addresses: [akka.tcp://application@192.168.0.152:2551]

Log4J2LoggerConfigurator.java

Will Sargent

unread,
Mar 29, 2016, 4:23:28 PM3/29/16
to play-framework
Hi,

I've created a sample project showing a modified play-scala template running with Log4J2 as the logging engine:


Will.

jsw...@ebay.com

unread,
Apr 14, 2016, 8:48:38 PM4/14/16
to play-framework
Is it possible to set the java property from build.scala or elsewhere in the project files? I tried

javaOptions in run += "-Dlog4j.configurationFile=conf/log4j2.xml"

but get the following via 'sbt run'

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

Matthias Kurz

unread,
Apr 15, 2016, 3:41:00 AM4/15/16
to play-framework
I also experience this behaviour.

Do you use play-java or play-scala?
Can you try if it works if you run your application with:
activator stage
./target/universal/stage/bin/<startscript>

?

Markus Hauck

unread,
Apr 15, 2016, 1:44:43 PM4/15/16
to play-framework
You have to also add 

fork in run := true

otherwise the javaOptions cannot be applied ;)


On Sunday, March 27, 2016 at 8:17:22 AM UTC+2, duluo...@gmail.com wrote:
I set ".enablePlugins(PlayJava).disablePlugins(PlayLogback)" and
  "org.apache.logging.log4j" % "log4j-slf4j-impl" % log4j2Version,
  "org.apache.logging.log4j" % "log4j-api" % log4j2Version,
  "org.apache.logging.log4j" % "log4j-core" % log4j2Version

then run with " -Dlog4j.configurationFile=conf/log4j2.xml ".

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

2016-03-27 14:16:01.671 INFO  play.core.server.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

Server started, use Alt+D to stop
2016-03-27 14:16:02.916 WARN  application - Logger configuration in conf files is deprecated and has no effect. Use a logback configuration file instead.
Mar 27, 2016 2:16:03 PM org.mongodb.morphia.logging.MorphiaLoggerFactory chooseLoggerFactory
INFO: LoggerImplFactory set to org.mongodb.morphia.logging.jdk.JDKLoggerFactory
2016-03-27 14:16:04.449 INFO  akka.event.slf4j.Slf4jLogger - Slf4jLogger started
2016-03-27 14:16:04.505 INFO  Remoting - Starting remoting
2016-03-27 14:16:04.610 INFO  Remoting - Remoting started; listening on addresses :[akka.tcp://application@192.168.0.152:2551]
2016-03-27 14:16:04.612 INFO  Remoting - Remoting now listens on addresses: [akka.tcp://application@192.168.0.152:2551]

Will Sargent

unread,
Apr 15, 2016, 2:55:54 PM4/15/16
to play-fr...@googlegroups.com
Hi all,

This is a new feature, so although it works in production, it's still not as complete as I'd like.  Contributions are welcome.

There's an annoying problem in the DevServerStart in SBT plugin in how the classloaders are applied -- it's not easy to work in a custom logging layer in without some refactoring.   For now, it's easier to fix by specifying any properties before starting the JVM proper.

There's a couple of bugs which don't seem major, but are worth keeping an eye on:


and if you want to call LoggerConfigurator, there isn't a Java API so you have to call "play.api.LoggerConfigurator$.MODULE$.apply(..)" 

Have filed bug for that as well:


And please --  if you run into issues with documentation include a better phrasing or point by point discussion of the gap in your bug report, otherwise it can be very hard to know what the gap or misunderstanding was.

Will.


2016-03-27 14:16:04.610 INFO  Remoting - Remoting started; listening on addresses :[akka.tcp://appli...@192.168.0.152:2551]
2016-03-27 14:16:04.612 INFO  Remoting - Remoting now listens on addresses: [akka.tcp://appli...@192.168.0.152:2551]

--
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.

Matthias Kurz

unread,
Apr 15, 2016, 4:02:10 PM4/15/16
to play-framework
I found another workaround so that the logger configurator class can be found even in DEV mode:
You can put it in a another (sub-)project (which you include via libraryDependencies). Looks like when it get packaged in a seperate jar file it works.
2016-03-27 14:16:04.610 INFO  Remoting - Remoting started; listening on addresses :[akka.tcp://application@192.168.0.152:2551]
2016-03-27 14:16:04.612 INFO  Remoting - Remoting now listens on addresses: [akka.tcp://application@192.168.0.152:2551]

Will Sargent

unread,
Apr 16, 2016, 9:00:13 PM4/16/16
to Matthias Kurz, play-fr...@googlegroups.com
Great!

Can you send a PR to https://github.com/wsargent/play-2.5-log4j2 or create your own project and I’ll incorporate it?

Will.

simin Ghasemi

unread,
Apr 25, 2016, 10:54:28 AM4/25/16
to play-framework

I also defined the Log4J2LoggerConfigurator class as described in the

https://github.com/wsargent/play-2.5-log4j2

 

then run with " -Dlog4j.configurationFile=conf/log4j2.xml ".

 

but for initializing my JDBC Appender, I`ve got following error:

2016-04-25 18:20:34,191 pool-4-thread-2 ERROR java.lang.ClassNotFoundException: com.irisaco.oes.playplugin.logging.biz.ConnectionFactory java.lang.ClassNotFoundException: com.irisaco.oes.playplugin.logging.biz.ConnectionFactory

                at java.net.URLClassLoader$1.run(URLClassLoader.java:372)

                at java.net.URLClassLoader$1.run(URLClassLoader.java:361)

                at java.security.AccessController.doPrivileged(Native Method)

                at java.net.URLClassLoader.findClass(URLClassLoader.java:360)

                at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

                at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

                at java.lang.Class.forName0(Native Method)

                at java.lang.Class.forName(Class.java:259)

                at org.apache.logging.log4j.util.LoaderUtil.loadClass(LoaderUtil.java:122)

                at org.apache.logging.log4j.core.util.Loader.loadClass(Loader.java:228)

                at org.apache.logging.log4j.core.appender.db.jdbc.FactoryMethodConnectionSource.createConnectionSource(FactoryMethodConnectionSource.java:82)

 

after tracing the code, It seems that class path of log4j is not the same as my application class path.

 

My project is attached.

 

Thanks a lot

log4j2-play2.5.rar
Reply all
Reply to author
Forward
0 new messages