log4j:ERROR Either File or DatePattern options are not set for appender [logfile].
web.xml 产生问题配置:
<context-param>
<param-name>webAppRootPathparam-name>
<param-value>webapp.rootparam-value>
context-param>
<context-param>
<param-name>log4jConfigLocationparam-name>
<param-value>/WEB-INF/classes/log4j.propertiesparam-value>
context-param>
<context-param>
<param-name>log4jRefreshIntervalparam-name>
<param-value>3000param-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListenerlistener-class>
listener>
log4j.properties 配置:
#DEBUG < INFO < WARN < ERROR < FATAL
log4j.rootLogger=DEBUG,console,logfile
#console configure
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[BIKE] %d{yyyy-MM-dd HH:mm:ss} [%5p] (%F:%L) - %m%n
log4j.appender.console.Encoding=UTF-8
#logfile configure
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.File=${webapp.root}/logs/log.txt
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern= %d %p [%c] - <%m>%n
log4j.appender.logfile.Threshold=INFO
log4j.appender.logfile.Encoding=UTF-8
log4j.logger.freemarker=FATAL
log4j.logger.org.springframework=WARN
log4j.logger.org.mybatis=DEBUG
log4j.logger.net.sf.ehcache=ERROR
解决方法:把
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListenerlistener-class>
listener>
移动所以
context-param 之后就行
解决后web.xml配置:
<context-param>
<param-name>webAppRootPathparam-name>
<param-value>webapp.rootparam-value>
context-param>
<context-param>
<param-name>log4jConfigLocationparam-name>
<param-value>/WEB-INF/classes/log4j.propertiesparam-value>
context-param>
<context-param>
<param-name>log4jRefreshIntervalparam-name>
<param-value>3000param-value>
context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListenerlistener-class>
listener>
猜想:
此问题与web.xml加载顺序有关 “context-param -> listener -> filter -> servlet” ;
特别说明 如果在web.xml中已经配置了org.springframework.web.util.Log4jConfigListener这个监听器,
则不需要配置WebAppRootListener了。因为Log4jConfigListener已经包含了WebAppRootListener的功能。
WebAppRootListener要在ApplicationContext的ContextLoaderListener之前, 否则ApplicationContext的bean注入根目录值时会发生无法注入异常