How can I determine where Log4j selects its configuration from

Is there any way to find out where Log4J selects the configuration file? I tried changing my log4j.xml and the changes were not reflected in the behavior of Log4j. I deleted log4j.xml and funny enough, Log4J is still working with the old behavior. Therefore, he should select the configuration file available in my namespace. But the question is, how can I find out which one. Is there any way to do this? there are many different dependencies on jars, etc., so one of them should contain log4j.xml or log4j.properties, which overrides my changes. Any ideas?

+3
source share
2 answers

-Dlog4j.debug. log4j , log4j.xml, :

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

, -Dlog4j.configuration=file:c:/pathToFile, .

+7

-Dlog4j.debug, FrVaBe , . , log4j. , log4j, URL- .

, - :

/**
 * Get the URL of the log4j config file. This is mostly copied from org.apache.log4j.LogManager default init routine.
 *
 * @return log4j config url
 */
public static URL getLog4JConfigurationUrl(){

    /** Search for the properties file log4j.properties in the CLASSPATH.  */
    String override = OptionConverter.getSystemProperty(LogManager.DEFAULT_INIT_OVERRIDE_KEY, null);

    // if there is no default init override, then get the resource
    // specified by the user or the default config file.
    if (override == null || "false".equalsIgnoreCase(override)){

      String configurationOptionStr = OptionConverter.getSystemProperty(LogManager.DEFAULT_CONFIGURATION_KEY, null);

      URL url;

      // if the user has not specified the log4j.configuration
      // property, we search first for the file "log4j.xml" and then
      // "log4j.properties"
      if (configurationOptionStr == null){
        url = Loader.getResource("log4j.xml");
        if (url == null){
          url = Loader.getResource(LogManager.DEFAULT_CONFIGURATION_FILE);
        }
      } else {
        try {
          url = new URL(configurationOptionStr);
        } catch (MalformedURLException ex) {
          // so, resource is not a URL:
          // attempt to get the resource from the class path
          url = Loader.getResource(configurationOptionStr);
        }
      }

      if (url == null)
        throw new RuntimeException("log4j configuration could not be found!");

      return url;
    }

    throw new RuntimeException("default init is overridden, log4j configuration could not be found!");
  }

PS: log4j , .

0

All Articles