【问题标题】:PropertyConfigurator.configure() (log4j 1.2.4) replacement in log4j 2.16Log4j 2.16 中的 PropertyConfigurator.configure() (log4j 1.2.4) 替换
【发布时间】:2022-01-20 06:48:02
【问题描述】:

我们在具有 log4j 1.x 版本的 java 项目中使用属性配置。客户要求从 1.x 版本升级到 2.16。我将 jar 文件从 log4j 1.2.4 替换为 log4j 2.16。我得到的唯一错误是

导入 org.apache.log4j.PropertyConfigurator;

在主函数中,我使用了 PropertyConfigurator.configure(property.getProperty("LOG_PATH"));

PropertyConfigurator 是 log4j 1.2.4 jar 中可用的类。 因为我用 log4j 2.16 jar 替换了它,所以这个 PropertyConfigurator 类不可用。 我的问题是:1)我应该怎么做才能摆脱这个错误。 2) log4j 2.16 中替代 PropertyConfigurator 类的替代类是什么。

我们完全只使用属性文件。不是 xml。请帮帮我。

【问题讨论】:

    标签: java log4j log4j2


    【解决方案1】:

    请参考以下 Log4J2 文档 https://logging.apache.org/log4j/2.0/faq.html#reconfig_from_code

    注意:确保将 log4j.xml/properties 文件重命名为 log4j2.xml/properties

    例子:

    import org.apache.logging.log4j.core.LoggerContext;
    
    public void configureLog4j() throws IOException
    {
      String path = getLog4jConfigFilePath();
      File file = new File(path);
      if (file.exists())
      {
        LoggerContext context  = (LoggerContext)LogManager.getContext(false);
            context.setConfigLocation(file.toURI());
      }else
      {
        throw new FileNotFoundException(path);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 1970-01-01
      • 2013-11-18
      • 2022-01-19
      • 1970-01-01
      • 2022-01-19
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多