java log4j 加载配置文件,log4j无法读取配置文件

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

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值