spring Log4j关于No appenders could be found for logger的警告
(spring环境下)配置Log4j时候,当启动WEB程序时,提示了如标题的警告,具体如下:
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
但是log的效果还在,仔细看异常发生在 (org.springframework.web.context.ContextLoader),即在ContextLoader时,spring framework需要使用log4j但此时log4j未寻找到其配置文件。其实解决方法,只要将log4j的listener放在spring context的前面就可以了。此外,如果按照默认的log4j配置文件位置也可以避免这个警告(src/log4j.properties,即WEB-INF/classes/log4j.properties),这是因为spring framework获取log时,log4j可以找到其配置文件了。
正确的配置文件片段:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file></welcome-file> </welcome-file-list> <!-- 配置log4j文件 --> <context-param> <param-name>webAppRootKey</param-name> <param-value>webapp.root</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/config/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>5000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- 指明spring配置文件在何处 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/lib/applicationContext.xml</param-value> </context-param> <!-- 监听加载spring的配置文件 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
早年同窗始相知,三载瞬逝情却萌。年少不知愁滋味,犹读红豆生南国。别离方知相思苦,心田红豆根以生。