log4j无法读取配置文件 [英] log4j Could not read configuration file

查看:542
本文介绍了log4j无法读取配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将日志记录添加到我正在处理的Java Web项目中.我遇到了一个我无法弄清的错误.

I am adding logging to a java web project I am working on. I have run into an error that I am unable to figure out.

我从tomcat得到的错误是:
log4j:ERROR Could not read configuration file [log4j.properties]. java.io.FileNotFoundException: log4j.properties (No such file or directory)

The error I am getting from tomcat is:
log4j:ERROR Could not read configuration file [log4j.properties]. java.io.FileNotFoundException: log4j.properties (No such file or directory)

我的课堂上有这个简单的方法:

I have this simple method in my class:

@RemotingInclude
public UserAccount save(UserAccount dataObject)
{
    PropertyConfigurator.configure("log4j.properties");
    logger.debug(dataObject.toString());

    return dao.save(dataObject);
}

当我查看webapps//WEB-INF/class文件夹时,确实看到了log4j.properties文件.当我部署到tomcat服务器并重新启动tomcat时,我确实看到已创建admin.log文件,但是没有写入任何内容.即使点击上面的方法.任何帮助,我们将不胜感激.

When I look in my webapps//WEB-INF/class folder I do see my log4j.properties file. When I deploy to my tomcat server and restart tomcat, I do see my admin.log file created, but nothing is written to it. Even after hitting the method above. Any help with this is greatly appreciated.

这是我的log4j.properties文件的当前内容:

This is the current contents of my log4j.properties file:

log4j.appender.AdminFileAppender=org.apache.log4j.FileAppender
log4j.appender.AdminFileAppender.File=admin.log
log4j.appender.AdminFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.AdminFileAppender.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n.
log4j.appender.ReportFileAppender=org.apache.log4j.FileAppender
log4j.appender.ReportFileAppender.File=report.log
log4j.appender.ReportFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ReportFileAppender.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n
log4j.logger.com.rottmanj.services=WARN,AdminFileAppender

推荐答案

引导Log4j的方法是错误的.通常是这样实现的方式:

That approach of bootstraping the Log4j is wrong. This is usually the way that is implemented:

import org.apache.log4j.Logger;

public class MyService {

    public UserAccount save(UserAccount dataObject) {
        logger.debug(dataObject.toString());

        return dao.save(dataObject);

    }

    private static Logger logger = Logger.getLogger(MyService.class);

}

通过这种方式,Log4j将自动在类路径的根目录中查找log4j.properties.

This way Log4j will automatically lookup for the log4j.properties in the root of the classpath.

这篇关于log4j无法读取配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆