如何使用属性文件配置log4j

如何让log4j获取属性文件。

我正在编写一个Java桌面应用程序,我想使用log4j。 在我的主要方法中,如果有这个:

PropertyConfigurator.configure("log4j.properties"); 

当我打开Jar时,log4j.properties文件位于同一目录中。

但我得到这个错误:

log4j:ERROR无法读取配置文件[log4j.properties]。 java.io.FileNotFoundException:log4j.properties(系统找不到指定的文件)

我究竟做错了什么?

我相信configure方法需要一个绝对路径。 无论如何,您还可以尝试首先加载Properties对象:

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

如果属性文件在jar中,那么你可以这样做:

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

以上假设log4j.properties位于jar文件的根文件夹中。

使用PropertyConfigurator.configure( String configFilename )时,它们是log4j库中的以下操作。

 Properties props = new Properties(); FileInputStream istream = null; try { istream = new FileInputStream(configFileName); props.load(istream); istream.close(); } catch (Exception e) { ... 

它读取失败,因为它从执行应用程序的当前目录中查找“Log4j.properties”。

如何更改属性文件的读取部分的方式如下,并将“log4j.properties”放在设置CLASSPATH的目录上。

 ClassLoader loader = Thread.currentThread().getContextClassLoader(); URL url = loader.getResource("log4j.properties"); PropertyConfigurator.configure(url); 

存在将“Log4j.properties”放入jar文件的另一种方法。

 jar xvf [YourApplication].jar log4j.properties 

刚设置-Dlog4j.configuration = file:log4j.properties为我工作。

然后log4j在应用程序的当前工作目录中查找文件 log4j.properties。

请记住,log4j.configuration是一个URL规范,因此如果要引用文件系统上的常规文件,即不在类路径上的文件,请在log4j.properties文件名前面添加“file:”!

最初我指定了-Dlog4j.configuration = log4j.properties。 但是,仅当log4j.properties位于类路径上时才有效。 当我将log4j.properties复制到我的项目中的main / resources并重建以便将其复制到目标目录(maven项目)时,这也很有用(或者你可以将log4j.properties打包到你的项目jar中,但那不会允许用户编辑记录器配置!)。

这是@kgiannakakis的答案编辑:原始代码错误,因为在调用Properties.load(InputStream)后它没有正确关闭InputStream。 从Javadocs: The specified stream remains open after this method returns.

================================

我相信configure方法需要一个绝对路径。 无论如何,您还可以尝试首先加载Properties对象:

 Properties props = new Properties(); InputStream is = new FileInputStream("log4j.properties"); try { props.load(is); } finally { try { is.close(); } catch (Exception e) { // ignore this exception } } PropertyConfigurator.configure(props); 

如果属性文件在jar中,那么你可以这样做:

 Properties props = new Properties(); InputStream is = getClass().getResourceAsStream("/log4j.properties"); try { props.load(is); } finally { try { is.close(); } catch (Exception e) { // ignore this exception } } PropertyConfigurator.configure(props); 

以上假设log4j.properties位于jar文件的根文件夹中。

我今天在我的应用程序中有这个代码

 File log4jfile = new File("./conf/log4j.properties"); PropertyConfigurator.configure(log4jfile.getAbsolutePath()); 

相对路径来自JVM的工作目录(JVM启动的位置)。

我相信它需要在java类路径中的log4j.properties目录。 在您的情况下,将CWD添加到类路径应该工作。

由于JVM参数最终作为系统变量传递给您的java程序,因此您可以在执行点的开头使用此代码来编辑属性,并让log4j读取您刚刚在系统属性中设置的属性

 try { System.setProperty("log4j.configuration", new File(System.getProperty("user.dir")+File.separator+"conf"+File.separator+"log4j.properties").toURI().toURL().toString()); } catch (MalformedURLException e) { e.printStackTrace(); } 

您可以通过定义’ log4j.debug ‘变量来启用log4j内部日志记录。