(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>