vertx3 logging with log4j

2,603 views
Skip to first unread message

Asher Tarnopolski

unread,
Oct 26, 2014, 12:41:40 PM10/26/14
to ve...@googlegroups.com
hi guys,

while playing with vertx3 i got stuck with a simple task of logging through log4j.
i have the following setup:

- gradle build that creates a fatjar
- log4j.xml is copied into /platform_lib within a jar

i'm running the fatjar from a command line as following:

java -Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.impl.Log4jLogDelegateFactory -Dlog4j.debug=true -jar vertx3-fun-0.0.1.jar -conf project.json

log4 reports that it actually fails to allocate the log4j.xml:

log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@5c647e05.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader@5c647e05 class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Could not find resource: [null].


i tried to play by moving log4j.xml into different locations within the jar, but unfortunately with no success.
i guess i'm missing something here.
please advise.  


 

Alexander Lehmann

unread,
Oct 26, 2014, 1:31:43 PM10/26/14
to ve...@googlegroups.com
I think for a vertx 3 fatjar, the log4j.xml file should just end up in the root of the jar, but I have to check.

Alexander Lehmann

unread,
Oct 26, 2014, 2:23:39 PM10/26/14
to ve...@googlegroups.com
The file as to be in main/src/resources for vertx3, it will end up in the root of the jar file, which puts it as /log4j.xml in the classpath.

I assume the platform_lib dir is no longer necessary since the jar is no longer started with a starter class.



On Sunday, October 26, 2014 5:41:40 PM UTC+1, Asher Tarnopolski wrote:

Asher Tarnopolski

unread,
Oct 26, 2014, 3:02:53 PM10/26/14
to ve...@googlegroups.com
yup, fatjar's root works.
thanks a lot, alexander!

Tim Fox

unread,
Oct 27, 2014, 3:02:54 AM10/27/14
to ve...@googlegroups.com
Vert.x 3 has a flat classpath, so you should be able to configure log4j in the normal log4j way (consult the log4j docs) nothing Vert.x specific here :)
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kaan Yamanyar

unread,
Oct 27, 2014, 3:41:25 AM10/27/14
to ve...@googlegroups.com
Hi,

May I recommend for you to use slf4j and logback.

We had some concurrency problems with log4j and slf4j + logback solved the problem.



27 Ekim 2014 Pazartesi 09:02:54 UTC+2 tarihinde Tim Fox yazdı:

Asher Tarnopolski

unread,
Oct 27, 2014, 4:31:54 AM10/27/14
to ve...@googlegroups.com

thanks for the info

Sent from my Nexus 4

You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/Nz5ERcyaEoA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vertx+un...@googlegroups.com.

Alan Ma

unread,
Aug 12, 2015, 12:45:13 AM8/12/15
to vert.x
I have a log4j.xml in src/main/resource and set the log level to debug. Everything works as expected when I debugging in IDE. When I run the jar on server, I'd like to use an external log4j.xml config to set the log level to error. So I'm using the following cmd to start my service:

java -jar my-vertx-app.jar -Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory -Dlog4j.configuration=file:./log4j.xml -conf ./config.json    

 However, no matter how I can try, the service still picking up the log4j.xml from the jar instead of specified log4j.xml. Any idea and suggestions?

Thanks in advance.

Asher Tarnopolski

unread,
Aug 12, 2015, 12:50:39 AM8/12/15
to vert.x
yoi have to implement log4j factory and pass the path to it instead of SLF4JLogDelegateFactory.

Alan Ma

unread,
Aug 12, 2015, 1:36:45 AM8/12/15
to vert.x
Thanks for the quick reply. However, still does not pick up the external log4j.xml after changing to io.vertx.core.logging.Log4jLogDelegateFactory. Vertx does not provide an easy process of using an external log config. sad.

Tim Fox

unread,
Aug 12, 2015, 2:08:52 AM8/12/15
to ve...@googlegroups.com
On 12/08/15 06:36, Alan Ma wrote:
Thanks for the quick reply. However, still does not pick up the external log4j.xml after changing to io.vertx.core.logging.Log4jLogDelegateFactory. Vertx does not provide an easy process of using an external log config. sad.

How is this related to Vert.x?

Vert.x does not know anything about log4j config. You should just be able to config how you would in any application. There's no magic here.


On Tuesday, August 11, 2015 at 9:50:39 PM UTC-7, Asher Tarnopolski wrote:
yoi have to implement log4j factory and pass the path to it instead of SLF4JLogDelegateFactory.
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.

Asher Tarnopolski

unread,
Aug 12, 2015, 4:01:47 AM8/12/15
to vert.x
you have to point -Dvertx.logger-delegate-factory-class-name to *YOUR* implementation of Log4jLogDelegateFactory. 

Alexander Lehmann

unread,
Aug 12, 2015, 6:51:28 AM8/12/15
to vert.x
The log4j framework should be configurable in vert.x in the same way as any other java program that uses it, the Logger interface only provides a mapping to the respective framework (plus it should work with slf4j in the same way as well).

I think I haven't used an external xml config yet (I think it worked with log4j.properties), I can try it out maybe later.

Alexander Lehmann

unread,
Aug 12, 2015, 7:42:37 AM8/12/15
to vert.x
Are you sure that log4j is searching for the local file in preference of the one in the classpath?
It would be better to have a file called something like log4j-prod.xml to avoid ambiguity between the two files.

Alan

unread,
Aug 12, 2015, 11:35:32 AM8/12/15
to vert.x
I tried chaning the specified log4j config to a different name. No luck. The issue seems that Dlog4j.configuration is not honored when running the fat jar.

java -jar my-vertx-app.jar -Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory -Dlog4j.configuration=file:./log4j-prof.xml -conf ./config.json

Alan

unread,
Aug 12, 2015, 11:37:33 AM8/12/15
to vert.x
Am using the following and still not picking up the external log4j config:

java -jar my-vertx-app.jar -Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.Log4jLogDelegateFactory -Dlog4j.configuration=file:./log4j-prof.xml -conf ./config.json

Alan

unread,
Aug 12, 2015, 11:41:46 AM8/12/15
to vert.x
Thanks for chiming in, Tim

I can use regular java jar with external slf4j/log4j/logback files without any issues. Only having issue with the vertx fat jar to run with external slf4j/log4j external config file. I honestly don't think this is vertx issue, but that's the fact I am observing. I searched google and could not find any example using command line option to set external log4j config with vertx 3 fat jar. Would you please be so kind to share an example if you have a working one? Thanks a ton.

I've tried the following and no luck:

java -jar my-vertx-app.jar -Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.Log4jLogDelegateFactory -Dlog4j.configuration=file:./log4j-prod.xml -conf ./config.json


java -jar my-vertx-app.jar -Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory -Dlog4j.configuration=file:./log4j-prod.xml -conf ./config.json

Asher Tarnopolski

unread,
Aug 12, 2015, 11:42:03 AM8/12/15
to ve...@googlegroups.com
try this after doing the appropriate changes:

java -Dvertx.logger-delegate-factory-class-name=your.class.extending.Log4jLogDelegateFactory
-Dlog4j.configurationFile=/full/path/to/log4j-prof.xml -jar
my-vertx-app.jar -conf /full/path/to/config.json
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "vert.x" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/vertx/Nz5ERcyaEoA/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> vertx+un...@googlegroups.com.
> Visit this group at http://groups.google.com/group/vertx.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/vertx/78cf2ea2-b095-4f02-9dda-be7d10652de3%40googlegroups.com.

Alan

unread,
Aug 12, 2015, 11:50:11 AM8/12/15
to vert.x
I truned on log4j.debug and the following is the output. The vertx fatjar always looking for the class path log4j config, instead of the using the java opt specified external log4j config file:

log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@55f96302.
log4j: Using URL [jar:file:/Users/path/to/my-vertx-app/target/my-vertx-app.jar!/log4j.xml] for automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
log4j: System property is :null
log4j: Standard DocumentBuilderFactory search succeded.
log4j: DocumentBuilderFactory is: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
log4j: debug attribute= "false".

How we could coerce the fatjar looking for the external log4j config file specified in the java opts? 
Thanks.

Tim Fox

unread,
Aug 12, 2015, 11:55:09 AM8/12/15
to ve...@googlegroups.com
Vert.x doesn't know anything about log4j or how log4j locates its config file.

Maybe you should push your project to github so others can take a look?
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.

Tim Fox

unread,
Aug 12, 2015, 11:56:37 AM8/12/15
to ve...@googlegroups.com
BTW. platform_lib is a Vert.x 2 thing, not used in Vert.x 3

Alan

unread,
Aug 12, 2015, 11:58:37 AM8/12/15
to vert.x
For sure. I'll push a reproducer soon. Thanks.

Alexander Lehmann

unread,
Aug 12, 2015, 3:22:22 PM8/12/15
to vert.x
I will do some tests later, but what strikes me as odd with the issue is that log4j should evaluate the same files and properties with any jar that is run with java -jar xxx.jar regardless of what project is in the jar (so it should either work with any or have the same issue with any)

Alexander Lehmann

unread,
Aug 12, 2015, 6:31:02 PM8/12/15
to vert.x
The order of the properties is relevant, they have to be before the -jar option

java -Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.Log4jLogDelegateFactory -Dlog4j.configuration=file:./log4j-prod.xml -jar my-vertx-app.jar -conf ./config.json

(to be fair, the solution was already mentioned in the other thread with the same topic, I just didn't quite get it before I wrote an example project to test it)

Alan

unread,
Aug 13, 2015, 11:53:00 AM8/13/15
to vert.x
Thanks Alexander. It didn't come to my mind the order would matter. And you're right on! I can confirm the external log4j config is picked up now. Just to repeat and for future questions like this, the following will be the right way to run with an external log4j config with either log4j or slf4j delegate:

java -Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.Log4jLogDelegateFactory -Dlog4j.configuration=file:./log4j-prod.xml -jar my-vertx-app.jar -conf ./config.json

java -Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory -Dlog4j.configuration=file:./log4j-prod.xml -jar my-vertx-app.jar -conf ./config.json

For lgoback, please use the following:


java 
-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory -Dlogback.configurationFile=file:./logback-prod.xml -jar my-vertx-app.jar -conf ./config.json

Thanks everyone for pitching in.
Reply all
Reply to author
Forward
0 new messages