Java 如何使用属性文件配置 log4j
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2288876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure log4j with a properties file
提问by Dan
How do I get log4j to pick up a properties file.
如何让 log4j 获取属性文件。
I'm writing a Java desktop app which I want to use log4j. In my main method if have this:
我正在编写一个我想使用 log4j 的 Java 桌面应用程序。在我的主要方法中,如果有这个:
PropertyConfigurator.configure("log4j.properties");
The log4j.properties file sits in the same directory when I open the Jar.
当我打开 Jar 时,log4j.properties 文件位于同一目录中。
Yet I get this error:
但我收到此错误:
log4j:ERROR Could not read configuration file [log4j.properties]. java.io.FileNotFoundException: log4j.properties (The system cannot find the file specified)
log4j:ERROR 无法读取配置文件 [log4j.properties]。java.io.FileNotFoundException: log4j.properties(系统找不到指定的文件)
What am I doing wrong?
我究竟做错了什么?
回答by kgiannakakis
I believe that the configure method expects an absolute path. Anyhow, you may also try to load a Properties object first:
我相信 configure 方法需要一个绝对路径。无论如何,您也可以尝试先加载一个 Properties 对象:
Properties props = new Properties();
props.load(new FileInputStream("log4j.properties"));
PropertyConfigurator.configure(props);
If the properties file is in the jar, then you could do something like this:
如果属性文件在 jar 中,那么您可以执行以下操作:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/log4j.properties"));
PropertyConfigurator.configure(props);
The above assumes that the log4j.properties is in the root folder of the jar file.
以上假设 log4j.properties 位于 jar 文件的根文件夹中。
回答by Jeach
You can enable log4j internal logging by defining the 'log4j.debug' variable.
您可以通过定义“ log4j.debug”变量来启用 log4j 内部日志记录。
回答by jmq
I have this code in my application today
我今天的应用程序中有此代码
File log4jfile = new File("./conf/log4j.properties");
PropertyConfigurator.configure(log4jfile.getAbsolutePath());
The relative path is from the working directory of the JVM (where the JVM starts).
相对路径来自 JVM 的工作目录(JVM 启动的位置)。
回答by Yu Sun corn
When you use PropertyConfigurator.configure(String configFilename), they are the following operation in the log4j library.
当您使用 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) {
...
It fails in reading because it looks for "Log4j.properties" from the current directory where the application is executed.
它无法读取,因为它从执行应用程序的当前目录中查找“Log4j.properties”。
How about the way that it changes the reading part of the property file as follows, and puts "log4j.properties" on the directory to which the CLASSPATH is set.
它改变属性文件的读取部分的方式如何,并将“log4j.properties”放在设置了CLASSPATH的目录中。
ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL url = loader.getResource("log4j.properties");
PropertyConfigurator.configure(url);
Another method of putting "Log4j.properties" in the jar file exists.
存在另一种将“Log4j.properties”放入 jar 文件的方法。
jar xvf [YourApplication].jar log4j.properties
回答by qwerty
I believe the log4j.properties directory it needs to be in the java classpath. In your case adding the CWD to the classpath shouldwork.
我相信 log4j.properties 目录需要在 java 类路径中。在您的情况下,将 CWD 添加到类路径应该可以工作。
回答by user2420019
Since JVM arguments are eventually passed to your java program as system variables, you can use this code at the beginning of your execution point to edit the property and have log4j read the property you just set in system properties
由于 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();
}
回答by kevinarpe
This is an edit of the answer from @kgiannakakis:
The original code is wrong because it does not correctly close the InputStream after Properties.load(InputStream)
is called. From the Javadocs: The specified stream remains open after this method returns.
这是@kgiannakakis 答案的编辑:原始代码是错误的,因为它在Properties.load(InputStream)
调用后没有正确关闭 InputStream 。来自 Javadocs:The specified stream remains open after this method returns.
================================
================================
I believe that the configure method expects an absolute path. Anyhow, you may also try to load a Properties object first:
我相信 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);
If the properties file is in the jar, then you could do something like this:
如果属性文件在 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);
The above assumes that the log4j.properties is in the root folder of the jar file.
以上假设 log4j.properties 位于 jar 文件的根文件夹中。
回答by consultantleon
just set -Dlog4j.configuration=file:log4j.properties worked for me.
只需设置 -Dlog4j.configuration=file:log4j.properties 为我工作。
log4j then looks for the filelog4j.properties in the current working directory of the application.
log4j 然后在应用程序的当前工作目录中查找文件log4j.properties。
Remember that log4j.configuration is a URLspecification, so add 'file:' in front of your log4j.properties filename if you want to refer to a regular file on the filesystem, i.e. a file not on the classpath!
记住 log4j.configuration 是一个URL规范,所以如果你想引用文件系统上的一个常规文件,即一个不在类路径上的文件,那么在你的 log4j.properties 文件名前面添加“文件:”!
Initially I specified -Dlog4j.configuration=log4j.properties. However that only works if log4j.properties is on the classpath. When I copied log4j.properties to main/resources in my project and rebuild so that it was copied to the target directory (maven project) this worked as well (or you could package your log4j.properties in your project jars, but that would not allow the user to edit the logger configuration!).
最初我指定了 -Dlog4j.configuration=log4j.properties。但是,这仅在 log4j.properties 位于类路径上时才有效。当我将 log4j.properties 复制到项目中的 main/resources 并重建以便将其复制到目标目录(maven 项目)时,这也有效(或者您可以将 log4j.properties 打包到项目 jar 中,但这不会允许用户编辑记录器配置!)。