转:http://blog.sina.com.cn/s/blog_7bbf356c01016wld.html
使用spring中的Log4jConfigListener有如如下好处:
1. 动态的改变记录级别和策略,不需要重启Web应用,如《Effective Enterprise Java》所说。
2. 把log文件定在 /WEB-INF/logs/ 而不需要写绝对路径。
因为 系统把web目录的路径压入一个叫webapp.root的系统变量。这样写log文件路径时不用写绝对路径了.
log4j.appender.logfile.File=${webapp.root}/WEB-INF/logs/myfuse.log
3. 可以把log4j.properties和其他properties一起放在/WEB-INF/ ,而不是Class-Path。
4.log4jRefreshInterval为6000表示 开一条watchdog线程每6秒扫描一下配置文件的变化;
在web.xml 添加
<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>6000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
<context-param> <param-name>webAppRootKey</param-name> <param-value>cang.qing6.com.root</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>6000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <context-param> <param-name>webAppRootKey</param-name> <param-value>cang.qing6.com.root</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>6000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
layoutPattern=[%d{HH:mm:ss}] %-5p : %m%n log.file=${message.web.root}/logs/app.log log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender log4j.appender.logfile.File=${log.file} log4j.appender.logfile.Append=true log4j.appender.logfile.DatePattern='.'yyyyMMdd log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=${layoutPattern} layoutPattern=[%d{HH:mm:ss}] %-5p : %m%n log.file=${message.web.root}/logs/app.log log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender log4j.appender.logfile.File=${log.file} log4j.appender.logfile.Append=true log4j.appender.logfile.DatePattern='.'yyyyMMdd log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=${layoutPattern}其实需要注意的地方就是应用服务器下有不止一个的应用在使用spring的Log4jConfigListener需要修改web环境中webAppRootKey值(这个值其实是web应用的根目录在环境变量名,这样在log4j配置文件中如果有相对web目录的路径就不用写死了)。
Log4J配置错误 - 关于webapp.root的设置
转:http://blog.csdn.net/cx921138/article/details/4736825
参考:
http://blogabc.googlecode.com/svn/trunk/doc/log4j.txt
http://jeiofw.blog.51cto.com/3319919/963145
近日,因为懒惰,直接从原有项目切出一个分块成了一个项目,然后同时发布启动,出现以下异常
Exception sending context initialized event to listener instance of class org.springframework.web.util.Log4jConfigListener java.lang.IllegalStateException: Web app root system property already set to different value: 'webapp.root' = [D:/tomcat-5.0.19/webapps/tzbms/] instead of [D:/tomcat-5.0.19/webapps/its/] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files! at org.springframework.web.util.WebUtils.setWebAppRootSystemProperty(WebUtils.java:99) at org.springframework.web.util.Log4jWebConfigurer.initLogging(Log4jWebConfigurer.java:116) at org.springframework.web.util.Log4jConfigListener.contextInitialized(Log4jConfigListener.java:51) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3773) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4270) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:866) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:850) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:638) at org.apache.catalina.core.StandardHostDeployer.install(StandardHostDeployer.java:320) at org.apache.catalina.core.StandardHost.install(StandardHost.java:875) at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:657) at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:476) at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1008) at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:394) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:166) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1134) at org.apache.catalina.core.StandardHost.start(StandardHost.java:832) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1126) at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:521) at org.apache.catalina.core.StandardService.start(StandardService.java:519) at org.apache.catalina.core.StandardServer.start(StandardServer.java:2345) at org.apache.catalina.startup.Catalina.start(Catalina.java:594) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:297) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:398)
public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException { String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM); String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY); String oldValue = System .getProperty(key); if (oldValue != null ) { throw new IllegalStateException ("WARNING: Web app root system property already set: " + key + " = " + oldValue + " - Choose unique webAppRootKey values in your web.xml files!" ); } String root = servletContext.getRealPath("/" ); if (root == null ) { throw new IllegalStateException ("Cannot set web app root system property when WAR file is not expanded"); } System .setProperty(key, root); servletContext.log("Set web app root system property: " + key + " = " + root); }