Spring提供了一个很实用的工具,可以让Web应用灵活配置log4j,这个工具类是:
org.springframework.web.util.Log4jConfigListener
org.springframework.web.util.Log4jConfigServlet
由于:
Note that this class has been deprecated for containers implementing
Servlet API 2.4 or higher, in favor of{@linkLog4jConfigListener}.</i><br>
According to Servlet 2.4, listeners must be initialized before load-on-startup
servlets. Many Servlet 2.3 containers already enforce this behavior
(see ContextLoaderServlet javadocs for details). If you use such a container,
this servlet can be replaced with Log4jConfigListener.
建议使用org.springframework.web.util.Log4jConfigListener,而非org.springframework.web.util.Log4jConfigServlet,下面来说下Log4jConfigListener的用法:
其实很简单,只要在web.xml文件中配置相关参数和注册此监听器即可,下面是相应的配置片段:
<!-- 配置log4j配置文件的路径,可以是xml或 properties文件(此参数必须配)-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/log4j.properties</param-value>
</context-param>
<!-- 每隔多少毫秒扫描一下配置文件的变化(此参数可选配) -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<!-- spring框架默认定义webAppRootKey的值为webapp.root,若不配此参数默认值就是webapp.root(因此,此参数可选配) -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>home</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
这样就可以在log4j的配置中如下进行了:
log4j.appender.DailyLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DailyLog.File=${home}/WEB-INF/logs/log4j.log
log4j.appender.DailyLog.Append=false
log4j.appender.DailyLog.DatePattern='.'yyyy-MM-dd
log4j.appender.DailyLog.layout=org.apache.log4j.PatternLayout
log4j.appender.DailyLog.layout.ConversionPattern=%p %d [%l]%n - %m%n
posted on 2009-07-25 03:34
jadmin 阅读(163)
评论(0) 编辑 收藏