How to get a .jar to recognise and use a bundled log4j.xml file?
I'm building a project with ant and during the build I'd like to b开发者_JS百科undle a log4j.xml
file directly into the .jar
to make distribution simpler. I've been successful in adding the file to the .jar
, but it doesn't seem to be recognised.
What do I need to do to ensure the file is recognised and used by log4j
? I'm new to Java, so a clear description, with examples if possible, would be great!
This would somehow works.
static // loads before anything else can
{
/*
* If log4j.configuration system property isn't set,
* then assume I'm inside a jar and configure
* log4j using the config file that shipped in the jar.
*/
if (System.getProperty("log4j.configuration") == null)
{
URL url = ClassLoader.getSystemResource("log4j.xml");
DOMConfigurator.configure(url);
}
}
How is your application being invoked?
As long as the JAR is on the classpath, any file contained in it is also in the classpath. In other words, log4j should have no problem in finding some.jar!log4j.xml
, if some.jar
is on the classpath to begin with.
Are there other log4j configuration files being picked up from other locations on the classpath? You can add -Dlog4j.debug
to the startup of your application to have log4j print out debugging information about which configuration file is being used. It is very possible that another configuration file bundled in another JAR is being found first.
精彩评论