【发布时间】:2014-02-12 16:15:17
【问题描述】:
我已将我的属性文件放在 Maven 项目的资源目录中。
然后我用属性配置器查看它:
PropertyConfigurator.configure("log4j.properties");
但我收到错误 log4j:ERROR 无法读取配置文件 [log4j.properties]。
【问题讨论】:
标签: java properties log4j
我已将我的属性文件放在 Maven 项目的资源目录中。
然后我用属性配置器查看它:
PropertyConfigurator.configure("log4j.properties");
但我收到错误 log4j:ERROR 无法读取配置文件 [log4j.properties]。
【问题讨论】:
标签: java properties log4j
你可以通过两种方式做到这一点
很可能该文件包含在 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 并在其中写入配置
【讨论】:
如果你想提供固定路径试试:-
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);
【讨论】: