How do I get log4j to pick up a properties file.
I\'m writing a Java desktop app which I want to use log4j. In my main method if have this:
Proper
When you use PropertyConfigurator.configure(String configFilename), they are the following operation in the log4j library.
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.
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.
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.
jar xvf [YourApplication].jar log4j.properties
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.
just set -Dlog4j.configuration=file:log4j.properties worked for me.
log4j then looks for the file log4j.properties in the current working directory of the application.
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!
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!).