【问题标题】:How can I tell where is Log4j picking its configuration from我怎么知道 Log4j 从哪里选择它的配置
【发布时间】:2011-05-20 11:02:29
【问题描述】:

有没有办法弄清楚 Log4J 从哪里挑选配置文件? 我试图更改我的 log4j.xml,但更改并未反映在 Log4j 行为中。我删除了 log4j.xml,有趣的是,Log4J 仍在处理旧的行为。所以它必须选择一些在我的命名空间中可用的配置文件。但问题是我怎样才能弄清楚是哪一个。有没有办法做到这一点? jar 等有很多不同的依赖项,因此其中之一必须包含覆盖我的更改的 log4j.xml 或 log4j.properties。 有什么想法吗?

【问题讨论】:

    标签: log4j


    【解决方案1】:

    运行您的应用程序时,您可以设置系统属性-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,如here 所述。

    【讨论】:

      【解决方案2】:

      FrVaBehis answer 中建议的那样激活-Dlog4j.debug 是一个很好的解决方案。但是,我想在启动时打印或记录位置而不激活 log4j 的调试模式。我创建了一个小 util 方法,它主要是 log4j 的默认 init 例程的副本,它返回配置文件的 URL。

      也许它对其他人有用:

      /**
       * 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:如果你知道如何向 log4j 询问当前使用的配置文件,请告诉我。

      【讨论】:

        猜你喜欢
        • 2014-03-23
        • 2012-07-02
        • 1970-01-01
        • 1970-01-01
        • 2017-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-29
        相关资源
        最近更新 更多