"No appenders could be found for logger"的错误

 

可能存在的情况:

(1)在tomcat\common\lib下多放了一个log4j,将其删除

That error is coming from commons-logging finding Log4j and using it in preference to j2sdk1.4.x 
logging, but if Log4j doesn't, then, find its configuration, you will get that error.

(2) 将log4j.properties所在的目录加到CLASSPATH中

(3)从WEB-INF\lib下复制log4j.jartomcat\common\lib

I just tested having log4j.jar in my WEB-INF/lib and not in common/lib and commons-logging in common/lib couldn't see it (and, therefore, didn't get the Log4j error), so it is most likely that you have log4j.jar in common/lib (or shared/lib) or some other place where commons-logging in Tomcat can see log4j.jar.        Remove Log4j.jar and you won't see that error....or, I suppose, you could add log4j.xml or log4j.properties to common/classes and all would be well.

(4)确认log4j.propertiestomcat\webapps\~\WEB-INF\classes下,~\src下,~\WebRoot\WEB-INF\classes

(5)一个多线程的程序,在子线程中会用到两个Logger,一个绑定到公共的日志输出,另一个则使用独立的日志文件,后者会在线程结束时关闭日志文件。开始我使用了Logger.shutdown(),正是这一方法导致了问题,原因在于shutdown会关闭所有在程序中使用到的Logger,甚至是RootLogger,同时释放Logger占用的所有资源,如Appender。这样一来自然其他的线程再向公共日志中输出时会报Appender不存在的错误。

             结论:如果希望显式地关闭日志,应该使用Appender.close()方法;对于FileAppender,可以使用CloseFile()方法。也可以直接使用Logger.removeAppender()方法强行移除Appender,但对于SocketAppender或AsyncAppender可能会导致pending logging。

(6)在 log4j.properties 中加入一个名为 org.springframework 的 Logger。例如:

log4j.logger.org.springframework=DEBUG, org.springframework, stdout
log4j.appender.org.springframework
=org.apache.log4j.FileAppender
log4j.appender.org.springframework.File
=/opt/adstat/log/spring.log
log4j.appender.org.springframework.Encoding
=utf8
log4j.appender.org.springframework.layout
=org.apache.log4j.PatternLayout
log4j.appender.org.springframework.layout.ConversionPattern
=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n

你可能感兴趣的:(Log4j Error)