开发者

How can I tell where is Log4j picking its configuration from

开发者 https://www.devze.com 2023-03-07 12:16 出处:网络
Is there a way to figure out where is Log4J picking the con开发者_如何学Cfiguration file from? I tried to change my log4j.xml and the changes were not reflected in Log4j behaviour. I deleted log4j.xml

Is there a way to figure out where is Log4J picking the con开发者_如何学Cfiguration file from? I tried to change my log4j.xml and the changes were not reflected in Log4j behaviour. I deleted log4j.xml and funnily enough, Log4J is still working with the old behaviour. So it must be picking some configuration file which is available in my namespace. But the question is how can I figure out which one. Is there a way to do that? there are so many different dependencies on jars etc, so one of them must contains a log4j.xml or log4j.properties that is overriding my changes. Any ideas?


When running your application you can set the system property -Dlog4j.debug. log4j will produce debug output in this case and tells you about how it resolves the path to the log4j.xml, e.g.:

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

To set a path explicit use the system property -Dlog4j.configuration=file:c:/pathToFile as described here.


Activating -Dlog4j.debug like suggested by FrVaBe in his answer is a good solution. However, I wanted to print or log the location on startup without activating log4j's debug mode. I created a little util method, that is mostly a copy of log4j's default init routine, which returns the URL of the configuration file.

Maybe it's useful for someone else:

/**
 * Get the URL of the log4j config file. This is mostly copied from org.apache.log4j.LogManager's 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: if you know a way to ask log4j for it's currently used configuration file, let me know.

0

精彩评论

暂无评论...
验证码 换一张
取 消