[java] How to configure log4j with a properties file

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:

   PropertyConfigurator.configure("log4j.properties");

The log4j.properties file sits in the same directory when I open the 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)

What am I doing wrong?

This question is related to java properties log4j

The answer is


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

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();
    }

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.

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

I believe that the configure method expects an absolute path. Anyhow, you may also try to load a Properties object first:

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:

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.


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 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).


I believe that the configure method expects an absolute path. Anyhow, you may also try to load a Properties object first:

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:

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.


import org.apache.log4j.PropertyConfigurator;

Import this, then:

Properties props = new Properties();
InputStream is = Main.class.getResourceAsStream("/log4j.properties");

try {
    props.load(is);
} catch (Exception e) {
    // ignore this exception
    log.error("Unable to load log4j properties file.",e);
}
PropertyConfigurator.configure(props);

My java files directory like this:

src/main/java/com/abc/xyz

And log4j directory like this:

src/main/resources

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.


You can enable log4j internal logging by defining the 'log4j.debug' variable.


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!).


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to properties

Property 'value' does not exist on type 'EventTarget' How to read data from java properties file using Spring Boot Kotlin - Property initialization using "by lazy" vs. "lateinit" react-router - pass props to handler component Specifying trust store information in spring boot application.properties Can I update a component's props in React.js? Property getters and setters Error in Swift class: Property not initialized at super.init call java.util.MissingResourceException: Can't find bundle for base name 'property_file name', locale en_US How to use BeanUtils.copyProperties?

Examples related to log4j

No log4j2 configuration file found. Using default configuration: logging only errors to the console How to stop INFO messages displaying on spark console? What is log4j's default log file dumping path How to give environmental variable path for file appender in configuration file in log4j No appenders could be found for logger(log4j)? SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". in a Maven Project log4j:WARN No appenders could be found for logger (running jar file, not web app) Error: "setFile(null,false) call failed" when using log4j java.lang.ClassNotFoundException: org.apache.log4j.Level How can I create 2 separate log files with one log4j config file?