简体   繁体   English

正确初始化log4j系统

[英]Properly initialize log4j system

I am trying to set up log4j for a project of mine. 我正在尝试为我的项目设置log4j。 I have found this tutorial, I followed each of its steps. 我找到了教程,我按照其每个步骤进行操作。

Of course, I have added the jar file to the Referenced Libraries . 当然,我已将jar文件添加到“ 参考库”中 The following picture shows that the path to the log.out file is in the PATH variable. 下图显示了log.out文件的路径在PATH变量中。 This is how my PATH variable looks like (just a snippet): 这是我的PATH变量的样子(只是一个片段):

在此处输入图片说明

The following picture shows that the path to the log4j jar file and the log4j.properties file is in the CLASSPATH variable. 下图显示了log4j jar文件和log4j.properties文件的路径在CLASSPATH变量中。 This is my CLASSPATH: 这是我的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): log4j.properties文件(与前面提到的教程的.properties示例相比,我唯一更改的是log4j.appender.FILE.File ):

在此处输入图片说明

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: 尝试遵循WARN消息中指定的链接时,我找到了以下答案:

This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration. 当找不到默认配置文件log4j.properties和log4j.xml并且应用程序不执行任何显式配置时,会发生这种情况。

How come the log4j.properties cannot be found? 为什么找不到log4j.properties? 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. 注意:我已经看到有很多有关这些特定WARN消息的文章,但是,我既没有maven项目,也没有动态Web项目。 This is a plain Java project and I am simply trying to test log4j. 这是一个普通的Java项目,我只是尝试测试log4j。 I have Windows 7 and use Eclipse Luna. 我有Windows 7,并使用Eclipse Luna。

UPDATE: It seems that if I move my log4j.properties file to the src folder, everything goes fine. 更新:看来,如果我将log4j.properties文件移动到src文件夹,一切都会很好。 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. 更新#2:我在下面标记为接受的答案回答了我来自更新的问题。 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. 默认情况下,Log4j在类路径的根目录下搜索配置文件( log4j.xmllog4j.properties )。 Putting it in the src folder automatically does that. 将其自动放入src文件夹即可。

If you do not wish to have it there, you can tell Log4j where to look for it through the system property log4j.configuration . 如果您不希望在此放置它,可以通过系统属性log4j.configuration告诉Log4j在哪里寻找它。 Launching your app then becomes : java -Dlog4j.configuration=file:"D:\\..." -jar myapp.jar . 然后启动您的应用程序: java -Dlog4j.configuration=file:"D:\\..." -jar myapp.jar In Eclipse, the system property is configured in "Run Configurations > Arguments > VM arguments". 在Eclipse中,系统属性是在“运行配置>参数> VM参数”中配置的。

By default, Log4j searches for a configuration file ( log4j.xml or log4j.properties ) at the root of your classpath. 默认情况下,Log4j在类路径的根目录下搜索配置文件( log4j.xmllog4j.properties )。 Putting it in the src folder automatically does that. 将其自动放入src文件夹即可。

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 我已经在log4j.xml中配置了log4j属性

//sample log4j.xml //样本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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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