Java – log4j: which config file was used

javalog4j

Using log4j, how can I find out what is the name and path oft the current DOMConfigurator file log4j.xml is used, to reset the file name using the PropertyConfigurator.configureAndWatch method, which needs the name and path of this file to see if it has changed.

The API document shows me how to configure log4j to reload the config, but I cannot find a way to see the filename and path it picked up automatically.
The application is running standalone withat any application server.

Thanks.

Best Answer

I am afraid you have no chance to get the automatically picked up path from the API. As I understand log4j's source code the detected path will just be used and not be stored.

At least you can use the -Dlog4j.debug property to output log4j-internal debugging information on startup and you will get some information like this:

log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@11b86e7.
log4j: Using URL [file:/C:/develop/workspace/foobar/target/classes/log4j.xml] for automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator

The 'log4j: Using URL ...' line comes from the LogManager class. You can check the initialization process from here. As I see the URL will not be stored for later information.