Properly initialize log4j system

Eszter

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.

Tunaki

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Java/JSP log4j:WARN Please initialize the log4j system properly

分類Dev

log4j対System.out.println-ロガーの利点?

分類Dev

Set Log4j 2 property file location without System.setProperty or classpath?

分類Dev

Log4j JDK Logging adapter: Apply LogManager system property late in the startup process

分類Dev

Javaの9 System.Loggerでのlog4j 2.10.0を使用します

分類Dev

Log4j AsyncAppender

分類Dev

Log4j StackOverflowError

分類Dev

OData V4 failing to properly serialize list of POCO(?)s containing a System.Object property

分類Dev

AndroidのLOG4J

分類Dev

Log4J Swing Appender

分類Dev

log4jの利点

分類Dev

同時log4j

分類Dev

Log4jとsyslogappender

分類Dev

Log4J動的構成

分類Dev

Log4j appender could not be instantiated

分類Dev

Log4j write nothing

分類Dev

TestNG、Junit、log4j

分類Dev

クラス名情報を保持しながら、System.out.printlnをLog4Jにリダイレクトします。

分類Dev

Mybatis log4j how to confiure log4j to print sql log to file

分類Dev

log4j 1.xとlog4j 2の混在

分類Dev

How to rotate a log4j log manually

分類Dev

Indent log4j messages based on their log level

分類Dev

Log4j: Different log levels for subpackages

分類Dev

log4j - Configure RollingFileAppender for backup log files as DailyRollingFileAppender

分類Dev

slf4j-log4j12とlog4jの違い

分類Dev

Log4j警告とClassNotFoundException問題

分類Dev

log4jでのRollingFileAppenderの構成

分類Dev

速度とlog4Jのエラー

分類Dev

初期化中のLog4j警告?