【问题标题】:How can I make Log4j find my properties如何让 Log4j 找到我的属性
【发布时间】:2014-02-12 16:15:17
【问题描述】:

我已将我的属性文件放在 Maven 项目的资源目录中。 然后我用属性配置器查看它: PropertyConfigurator.configure("log4j.properties");

但我收到错误 log4j:ERROR 无法读取配置文件 [log4j.properties]。

【问题讨论】:

    标签: java properties log4j


    【解决方案1】:

    你可以通过两种方式做到这一点

    很可能该文件包含在 jar 中,所以

    Properties props = new Properties();
    props.load(getClass().getResourceAsStream("/log4j.properties"));
    PropertyConfigurator.configure(props);
    

    如果不是

    Properties props = new Properties();
    props.load(new FileInputStream("log4j.properties"));
    PropertyConfigurator.configure(props);
    

    在 pom.xml 中你需要放入以下内容

     <log4jProperties>path/log4j.properties</log4jProperties>
    

    在该路径下,您需要创建 log4j.properties 并在其中写入配置

    【讨论】:

      【解决方案2】:

      如果你想提供固定路径试试:-

      Properties props = new Properties();
      
      try {
      
          File file = new File("C:/log4j/log4j.properties");
      
          props.load(new FileInputStream(file));
      
      } 
      
      catch (FileNotFoundException ex) {
          java.util.logging.Logger.getLogger(KGLMainFrame.class.getName()).log(Level.SEVERE, null, ex);
      
      } 
      catch (IOException ex) {
              java.util.logging.Logger.getLogger(KGLMainFrame.class.getName()).log(Level.SEVERE, null, ex);
      }
      
      PropertyConfigurator.configure(props);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-01
        • 2012-08-26
        • 1970-01-01
        • 1970-01-01
        • 2021-11-19
        • 1970-01-01
        相关资源
        最近更新 更多