1.问题
在junit测试中使用Logger对象无法输出日志,报错信息为:
log4j:WARN No appenders could be found for logger (tps.common.main.data.CodeMapper.getCodeList). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
2.解决方案
<方案一>在测试方法中加入如下代码:
BasicConfigurator.configure();
<方案二>在你的 log4j.properties 方法中加入如下配置信息:
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
参考资料:
1. http://stackoverflow.com/questions/12532339/no-appenders-could-be-found-for-loggerlog4j