Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make log4j display which file it used to configure itself?

Tags:

java

log4j

Question

Is it possible to make Log4J display the full path of the file it used for configuration?


Background

I have a love-hate relationship with log4j. In good times, it's great but when it's not working, it can be one of the most difficult things to debug. I manage all the logging in our application. As such, I'm very familiar with logging and the default initialization procedure defined in the manual. Still, it seems that, every few weeks, logging breaks and I spend a lot of time sorting out the problem.

This time, it's severely broken. Every single log statement everywhere is being dumped to the console and I can't figure out why. The same exact code base that used my log4j.xml files last week, is suddenly using some other configuration. Nothing obvious has changed. My only guess is, a few dependencies have changed and I suspect Maven has downloaded some evil JAR that's breaking everything.

If I could just figure out which configuration file Log4J decided to use at startup, I could tackle this and most other problems easily.


Summary

Is there some way to tell Log4J to print which file it used for configuration? Alternatively, is there a way to break a running application and use the debugger to answer this question (maybe with an expression or by inspecting variables)?

like image 567
gMale Avatar asked Sep 20 '10 15:09

gMale


People also ask

How do I know which log4j file to use?

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.

How do I change my log4j configuration?

The simplest is to put a log4j. xml on the classpath. Then add the monitorInterval="30" attribute to the opening configuration tag. This tells log4j to check the file for changes every 30 seconds.

What is the name of the log4j properties configuration file?

So the format for log4j properties file appender is log4j. appender.

Which log4j xml is used?

<appender-ref ref="console" /> </root> </log4j:configuration>


2 Answers

Yes, simply add log4j.debug to the JVM system variables. For example:

java -Dlog4j.debug -cp ... some.class.name 

Log4j will then output something like the following:

log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@1f7182c1.   log4j: Using URL [file:/C:/Users/matt/workspace/projectname/target/test-classes/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: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl   log4j: debug attribute= "null".   log4j: Ignoring debug attribute.   log4j: reset attribute= "false".   log4j: Threshold ="null".   ... 

For reference see the manual and the FAQ.

like image 160
matt b Avatar answered Sep 21 '22 02:09

matt b


I am using Log4J2 and if you want to get the file from inside your Java Program this worked for me:

LoggerContext lContect = LogManager.getContext(); Field f = lContect.getClass().getDeclaredField("configuration"); f.setAccessible(true); XmlConfiguration iWantThis = (XmlConfiguration) f.get(lContect); System.out.println("Config File: " + iWantThis.getName()); 
like image 44
Ben Avatar answered Sep 22 '22 02:09

Ben