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

后端 未结 3 1900
清酒与你
清酒与你 2020-12-07 17:41

Question

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


Background

I have a

相关标签:
3条回答
  • 2020-12-07 17:51

    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());
    
    0 讨论(0)
  • 2020-12-07 17:55

    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.

    0 讨论(0)
  • 2020-12-07 18:11

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

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

    0 讨论(0)
提交回复
热议问题