How do I override a Maven Plugin's log4j Configuration?
Asked Answered
C

1

6

I'm using the Cargo Maven plugin to deploy a WAR to a remote server, and I'm having problems. I'll probably create a second question for that problem, but this one is about overriding a Maven plugin's log4j configuration. Cargo uses JBoss' client libraries to ship stuff to JBoss servers (which I'm trying to do.) The JBoss library uses log4j. Cargo doesn't set up any sort of mapping layer that I know of.

So, essentially, log messages are occurring in the dependent library of a Maven plugin. I tried setting -Dlog4j.debug and got this information:

log4j: Trying to find [log4j.xml] using ClassRealm[plugin>org.codehaus.cargo:cargo-maven2-plugin:1.0.5, parent: ClassRealm[maven.api, parent: null]] class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader ClassRealm[plugin>org.codehaus.cargo:cargo-maven2-plugin:1.0.5, parent: ClassRealm[maven.api, parent: null]].
log4j: Using URL [jar:file:/C:/Users/username/.m2/repository/org/jboss/jbossts/jbossjts/4.13.1.Final/jbossjts-4.13.1.Final.jar!/log4j.properties] for automatic log4j configuration.
log4j: Reading configuration from URL jar:file:/C:/Users/username/.m2/repository/org/jboss/jbossts/jbossjts/4.13.1.Final/jbossjts-4.13.1.Final.jar!/log4j.properties
log4j: Could not find root logger information. Is this OK?
log4j: Parsing for [com.arjuna] with value=[INFO, default, stdout].
log4j: Level token is [INFO].
log4j: Category com.arjuna set to INFO
log4j: Parsing appender named "default".
log4j: Parsing layout options for "default".
log4j: Setting property [conversionPattern] to [%d [%t] %-5p %c - %m%n].
log4j: End of parsing for "default".
log4j: Setting property [maxBackupIndex] to [2].
log4j: Setting property [file] to [transaction.log].
log4j: Setting property [maxFileSize] to [500KB].
log4j: setFile called: transaction.log, true
log4j: setFile ended
log4j: Parsed "default" options.
log4j: Parsing appender named "stdout".
log4j: Parsing layout options for "stdout".
log4j: Setting property [conversionPattern] to [%d [%t] %-5p %c - %m%n].
log4j: End of parsing for "stdout".
log4j: Setting property [threshold] to [WARN].
log4j: Parsed "stdout" options.
log4j: Handling log4j.additivity.com.arjuna=[null]
log4j: Finished configuring.
log4j:WARN No appenders could be found for logger (org.jnp.interfaces.TimedSocketFactory).
log4j:WARN Please initialize the log4j system properly.

Then I tried setting -Dlog4j.configuration=mylog4j.properties but got this:

log4j: Trying to find [mylog4j.properties] using context classloader ClassRealm[plugin>org.codehaus.cargo:cargo-maven2-plugin:1.0.5, parent: ClassRealm[maven.api, parent: null]].
log4j: Trying to find [mylog4j.properties] using ClassRealm[plugin>org.codehaus.cargo:cargo-maven2-plugin:1.0.5, parent: ClassRealm[maven.api, parent: null]] class loader.
log4j: Trying to find [mylog4j.properties] using ClassLoader.getSystemResource().
log4j: Could not find resource: [mylog4j.properties].
log4j:WARN No appenders could be found for logger (org.jnp.interfaces.TimedSocketFactory).
log4j:WARN Please initialize the log4j system properly.

It would appear that the log4j configuration file needs to be in the Maven plugin's classpath, but I don't know how to include random files there, only dependencies. I really want to set this information so that I can get debug information from the JBoss libraries in order to troubleshoot my problem.

Childbirth answered 23/12, 2010 at 17:18 Comment(1)
The extended stack trace of the problem did, in fact, help me identify where the exception was being generated (and I'm slowly working on resolving it.) Still, seems like it might be useful information to know how to do this override, if it's even possible.Childbirth
G
10

With log4j.configuration you will set a resource string. Have a look at the log4j default initialization process and the examples.

If you use a syntax like this

-Dlog4j.configuration=mylog4j.properties

the file is expected in the classpath. But you can also use a syntax like this

-Dlog4j.configuration=file:/<path>/mylog4j.properties

to specify a fully qualified file location.

Guidepost answered 23/8, 2011 at 19:19 Comment(3)
Thanks for this. Is there a way to do this without adding anything to the maven command line? i.e. configuring the file location to be on the plugin's classpath?Paleoecology
@nat k log4j per default looks at the classpath to find a configuration file (Default Initialization) so it should be enough to put the configuration file in src/main/resources. If it conflicts with other files you should set the log4j.configuration property. It should be possile to to this in the pom.xml so you do not need the command line.Guidepost
Thanks for that. I thought so too, but with the plugin I am using (maven-docbkx-plugin), the log4j config file is not being picked up from src/main/resources. I ended up using the maven-set-properties plugin to set the log4j.configuration variable.Paleoecology

© 2022 - 2024 — McMap. All rights reserved.