0

I am trying to set up log4j for a project of mine. I have found this tutorial, I followed each of its steps.

Of course, I have added the jar file to the Referenced Libraries. The following picture shows that the path to the log.out file is in the PATH variable. This is how my PATH variable looks like (just a snippet):

enter image description here

The following picture shows that the path to the log4j jar file and the log4j.properties file is in the CLASSPATH variable. This is my CLASSPATH:

enter image description here

log4j.properties file (the only thing I changed is the log4j.appender.FILE.File in comparison to the .properties example from the tutorial previously mentioned):

enter image description here

This is how my project hierarchy looks like:

enter image description here

I am testing it with the following class:

package test;

import org.apache.log4j.Logger;

import java.io.*;
import java.sql.SQLException;

public class Log4jExample{
    /* Get actual class name to be printed on */
    static Logger log = Logger.getLogger(Log4jExample.class.getName());

    public static void main(String[] args) throws IOException,SQLException{

        log.debug("Hello this is an debug message");
        log.info("Hello this is an info message");
    }
}

I keep receiving the following exceptions and nothing is being logged to the file specified:

enter image description here

When trying to follow the link specified in the WARN message, I have found the following answer:

This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration.

How come the log4j.properties cannot be found? How can this be solved?

NOTE: I have seen that there are many posts regarding these particular WARN messages, however, I have neither a maven project, nor a dynamic web project. This is a plain Java project and I am simply trying to test log4j. I have Windows 7 and use Eclipse Luna.

UPDATE: It seems that if I move my log4j.properties file to the src folder, everything goes fine. How could I modify things to work from the current structure of files?

UPDATE #2: The answer I marked as accepted below answers my question from UPDATE. However, I have also found this post that proposes an useful solution to this problem with the location.

Community
  • 1
  • 1
Eszter
  • 331
  • 1
  • 3
  • 19

2 Answers2

4

By default, Log4j searches for a configuration file (log4j.xml or log4j.properties) at the root of your classpath. Putting it in the src folder automatically does that.

If you do not wish to have it there, you can tell Log4j where to look for it through the system property log4j.configuration. Launching your app then becomes : java -Dlog4j.configuration=file:"D:\..." -jar myapp.jar. In Eclipse, the system property is configured in "Run Configurations > Arguments > VM arguments".

Tunaki
  • 132,869
  • 46
  • 340
  • 423
0

By default, Log4j searches for a configuration file (log4j.xml or log4j.properties) at the root of your classpath. Putting it in the src folder automatically does that.

Dhiraj Thakur
  • 338
  • 3
  • 11