[英]Log4j printing to console every time
I have placed following log4j.properties file in src folder of my eclipse dynamic web project. 我已将以下log4j.properties文件放置在我的Eclipse动态Web项目的src文件夹中。 But everytime logs are getting printed on console though I am writing code to print the logs in file only. 但是每次日志都在控制台上打印时,尽管我正在编写代码以仅在文件中打印日志。 If I am deleting log4j.properties file then also it is getting printed on console.Don't know what went wrong. 如果我要删除log4j.properties文件,那么它也会被打印在控制台上。不知道出了什么问题。
###############################################################################
log4j.rootLogger=INFO, file
###############################################################################
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:/apache-tomcat-7.0.47/webapps/LogFile.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %-5p %c - %m%n
###############################################################################
I am using following code to print the logs. 我正在使用以下代码来打印日志。
import org.apache.log4j.Logger;
Logger log = Logger.getLogger(Client.class.getName());
log.info("First log");
Try to override the log4j configuration in your class like this: 尝试像这样在类中覆盖log4j配置:
import org.apache.log4j.PropertyConfigurator;;
static{
PropertyConfigurator.configure("src/main/resources/log4j.properties");
}
This assumes you have a file called log4j.properties
under src/main/resources
假设您在src/main/resources
下有一个名为log4j.properties
的文件
Thanks Nikhil 谢谢Nikhil
I have used follwing code by looking your code and it worked fine. 我通过查看您的代码使用了下面的代码,它工作正常。
import org.apache.log4j.PropertyConfigurator;
PropertyConfigurator.configure(getClass().getClassLoader().getResourceAsStream("log4j.properties"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.