log4j:WARN No appenders could be found for logger

log4j.properties文件放在了src下的main/resources中,执行程序就报如题的错误;

解决方法:建一个与src同级的文件夹config,把文件log4j.properties放进去就可以解决console输入日志的问题;

下面介绍下怎么把日志打印到日志文件中:

1.logconstant文件

public class LogConstant {
	public static final Logger alarmlog = Logger.getLogger("alarm.log");
	public static final Logger buglog  = Logger.getLogger("bug.log");
	public static final Logger runlog  = Logger.getLogger("run.log");
	
}

2.在主程序中需要加上如下语句:

LogConstant.alarmlog.info("line=" + line);

3.log4j.properties文件的内容

#log4j.rootLogger=INFO,console
log4j.rootLogger=DEBUG,console,runAppender

#write log to console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d - %c [%t] %-5p %c %x - %m%n

#write log to file
log4j.category.run.log = INFO,runAppender
log4j.additivity.run.log=true
log4j.appender.runAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.runAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.runAppender.layout.ConversionPattern=%d - %c [%t] %-5p %c %x %l - %m%n
log4j.appender.runAppender.DatePattern=.yyyy-MM-dd-HH
log4j.appender.runAppender.File=log/alarm.log


log4j.logger.org.springframework=ERROR
log4j.logger.com.opensymphony=ERROR
log4j.logger.com.ibatis=ERROR
log4j.logger.java.sql=ERROR
log4j.logger.java.sql.Statement=ERROR
log4j.logger.java.sql.PreparedStatement=ERROR
log4j.logger.java.sql.ResultSet=ERROR
log4j.logger.org.logicalcobwebs.proxool=ERROR
log4j.logger.freemarker=ERROR
log4j.logger.org.apache.commons=ERROR
log4j.logger.com.danga.MemCached=WARN
注意"
log4j.rootLogger=DEBUG,console,runAppender
"的写法,如果不这样写,只会生成空的日志文件,日志不会打印到日志文件中

你可能感兴趣的:(log4j:WARN No appenders could be found for logger)