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.
The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file. The level of the root logger is defined as INFO and attaches the ROLLINGFILE appender to it.
The log4j. properties file is a log4j configuration file which keeps properties in key-value pairs. By default, the LogManager looks for a file named log4j. properties in the CLASSPATH. The level of the root logger is defined as DEBUG.
xml parameters, Locate the log4j. xml file under the oarm/WEB-INF/classes/ directory. Update the log output path for each appender.
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.
You can use same process as log4j use on static initialization at LogManager class. Be aware of other initializations and external configurations e.g. from Spring's org.springframework.web.util.Log4jConfigListener
.
public static URL getLog4jConfig() { String override = OptionConverter.getSystemProperty("log4j.defaultInitOverride", null); if (override == null || "false".equalsIgnoreCase(override)) { String configurationOptionStr = OptionConverter.getSystemProperty("log4j.configuration", null); URL url; if (configurationOptionStr == null) { url = Loader.getResource("log4j.xml"); if (url == null) { url = Loader.getResource("log4j.properties"); } } else { try { url = new URL(configurationOptionStr); } catch (MalformedURLException ex) { url = Loader.getResource(configurationOptionStr); } } return url; } else { return null; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With