简体   繁体   中英

Properly initialize log4j system

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

在此处输入图片说明

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:

在此处输入图片说明

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

在此处输入图片说明

This is how my project hierarchy looks like:

在此处输入图片说明

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:

在此处输入图片说明

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.

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

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.

try this:

  private static Logger logger = LogManager.getLogger(<<YOURCLASSNAME>>.class.getName()); public static void main(String[] args) { /* * use DOMConfigurator class * to initialize the log4j environment * using a DOM tree * */ DOMConfigurator.configure("log4j.xml"); logger.info("your info"); 

I have configured log4j properties in log4j.xml

//sample log4j.xml

 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false"> <appender name="fileAppender" class="org.apache.log4j.FileAppender"> <param name="Threshold" value="INFO" /> <param name="File" value="logfile.log" /> <layout class="org.apache.log4j.PatternLayout" > <param name="ConversionPattern" value="%d %-5p [%c] %m %n" /> </layout> </appender> <root> <level value="INFO"/> <appender-ref ref="fileAppender"/> </root> </log4j:configuration> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM