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:
我正在编写一个Java桌面应用程序,我想使用log4j。在我的主要方法中,如果有:
PropertyConfigurator.configure("log4j.properties");
The log4j.properties file sits in the same directory when I open the Jar.
log4j。当我打开Jar时,属性文件位于相同的目录中。
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:错误不能读取配置文件[log4j.properties]。. io .FileNotFoundException:log4j。属性(系统无法找到指定的文件)
What am I doing wrong?
我做错了什么?
8 个解决方案
#1
43
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。属性位于jar文件的根文件夹中。
#2
12
When you use PropertyConfigurator.configure(String configFilename), they are the following operation in the log4j library.
当你使用PropertyConfigurator。配置(字符串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”。属性“从应用程序执行的当前目录”。
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”。在设置类路径的目录上的属性。
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”。jar文件中的属性存在。
jar xvf [YourApplication].jar log4j.properties
#3
5
just set -Dlog4j.configuration=file:log4j.properties worked for me.
设置-Dlog4j.configuration =文件:log4j。属性为我工作。
log4j then looks for the file log4j.properties in the current working directory of the application.
然后,log4j查找文件log4j。应用程序当前工作目录中的属性。
Remember that log4j.configuration is a URL specification, 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。配置是一个URL规范,所以在您的log4j前面添加“file:”。如果您想要引用文件系统中的常规文件,即不在类路径上的文件,则属性文件名。
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中才有用。属性位于类路径上。当我复制log4j。我的项目中的主要/资源的属性和重建,使它被复制到目标目录(maven项目),这也起作用了(或者您可以打包您的log4j)。您的项目jar中的属性,但是不允许用户编辑日志记录器的配置!
#4
3
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的回答的编辑:原始代码是错误的,因为它没有正确地关闭属性后的InputStream。从Javadocs:在此方法返回后,指定的流仍然是打开的。
================================
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
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。属性位于jar文件的根文件夹中。
#5
1
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启动的地方)。
#6
0
I believe the log4j.properties directory it needs to be in the java classpath. In your case adding the CWD to the classpath should work.
我相信log4j。属性目录需要在java类路径中。在您的案例中,将CWD添加到类路径应该会有用。
#7
0
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();
}
#8
-1
You can enable log4j internal logging by defining the 'log4j.debug' variable.
通过定义“log4j.debug”变量,您可以启用log4j内部日志记录。
#1
43
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。属性位于jar文件的根文件夹中。
#2
12
When you use PropertyConfigurator.configure(String configFilename), they are the following operation in the log4j library.
当你使用PropertyConfigurator。配置(字符串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”。属性“从应用程序执行的当前目录”。
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”。在设置类路径的目录上的属性。
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”。jar文件中的属性存在。
jar xvf [YourApplication].jar log4j.properties
#3
5
just set -Dlog4j.configuration=file:log4j.properties worked for me.
设置-Dlog4j.configuration =文件:log4j。属性为我工作。
log4j then looks for the file log4j.properties in the current working directory of the application.
然后,log4j查找文件log4j。应用程序当前工作目录中的属性。
Remember that log4j.configuration is a URL specification, 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。配置是一个URL规范,所以在您的log4j前面添加“file:”。如果您想要引用文件系统中的常规文件,即不在类路径上的文件,则属性文件名。
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中才有用。属性位于类路径上。当我复制log4j。我的项目中的主要/资源的属性和重建,使它被复制到目标目录(maven项目),这也起作用了(或者您可以打包您的log4j)。您的项目jar中的属性,但是不允许用户编辑日志记录器的配置!
#4
3
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的回答的编辑:原始代码是错误的,因为它没有正确地关闭属性后的InputStream。从Javadocs:在此方法返回后,指定的流仍然是打开的。
================================
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
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。属性位于jar文件的根文件夹中。
#5
1
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启动的地方)。
#6
0
I believe the log4j.properties directory it needs to be in the java classpath. In your case adding the CWD to the classpath should work.
我相信log4j。属性目录需要在java类路径中。在您的案例中,将CWD添加到类路径应该会有用。
#7
0
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();
}
#8
-1
You can enable log4j internal logging by defining the 'log4j.debug' variable.
通过定义“log4j.debug”变量,您可以启用log4j内部日志记录。