log4j Could not read configuration file

时光怂恿深爱的人放手 提交于 2019-12-10 15:29:40

问题


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.

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);
}

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.

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

回答1:


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

}

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



来源:https://stackoverflow.com/questions/6528287/log4j-could-not-read-configuration-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!