Is it possible to make log4j, which file did it use to configure?

Question

Is it possible for Log4J to display the full path to the file that it used to configure?




Background

I have a love-hate relationship with log4j. This is great in good times, but when it doesn't work, it can be one of the most difficult things to debug. I manage all the inputs to our application. Thus, I am very familiar with the logging and default initialization procedure defined in the manual. However, it seems that every few weeks the registration is interrupted, and I spend a lot of time sorting the problem.

This time it is badly broken. Each log statement is always reset to the console, and I cannot understand why. The same exact code base that used my log4j.xml files last week suddenly uses a different configuration. Nothing obvious has changed. My only assumption: several dependencies have changed, and I suspect that Maven has loaded some kind of evil JAR that crashed everything.

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




Summary

Is it possible to specify Log4J to print the file that it used to configure? Alternatively, is there a way to break a running application and use a debugger to answer this question (perhaps with an expression or by checking variables)?

+52
java log4j
Sep 20 '10 at 15:26
source share
3 answers

Yes, just 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". ... 

See the manual and FAQ for reference.

+56
Sep 20 '10 at 15:37
source share

I am using Log4J2, and if you want to get the file from 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()); 
0
Jan 18 '16 at 17:38
source share

The designation of log4j 2 for this is to set <Configuration status="trace"> in the configuration file. This will display the location where the log4j2 configuration file is loaded, as well as other internal data of the log4j2 configuration process. The default state logger level is WARN, so you only see alerts when a problem occurs.

If the configuration file is not found correctly, you can still enable the log2j2 internal status log by setting the system property org.apache.logging.log4j.simplelog.StatusLogger.level to TRACE .

0
Jan 19 '16 at 12:45
source share



All Articles