spring中的Log4jConfigListener作用和webapp.root的设置
转: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>
在使用spring先后开发了两个模块,单独测试都正常。也先后上线运行,之后发现有个模块在启动Tomcat后总是初始化失败,必须到tomcat管理控制台手动启动。找了半天也没发现原因。后来管理员在每次重启Tomcat后这个模块没有运行导致一堆问题和麻烦,今天特意查看了其他的tomcat日志文件,终于发现了问题所在,原来是Log4jConfigListener。使用它是为了随时调整打印日志的级别而不用重启服务。没想到没有享受到它的便利,反而出了一堆问题,只能怪自己没有稍微仔细研究一下。
- <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>
log4j.properties配置
- 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目录的路径就不用写死了)。
否则两个默认值web.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)
看看异常,还是挺简单的,应该是两个项目的设置重复了,导致出错,但我发现web.xml里并没有配置webAppRootKey项,原来是因为如果没有web.xm 内没有设置webAppRootKey项,是为默认设置
- 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);
- }
从代码看出,该方法其实就是把该web application的根目录的绝对文件路径作为属性保存在
System的属性列表中。该属性的名字,由web.xml文件中的名为"webAppRootKey"的参数值指出。如果不在web.xml中定义
webAppRootKey参数,那么属性名就是缺省的"webapp.root".在我们的petclinic项目中已经定义了
webAppRootKey参数,其值为"petclinic.root",因此,属性的名字就是"petclinic.root".
posted on 2016-11-04 15:13 nickTimer 阅读(6455) 评论(0) 编辑 收藏 举报