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".
spring中的Log4jConfigListener作用和webapp.root的设置的更多相关文章
- Spring 中bean的作用、定义
Spring 中bean的作用.定义: 创建一个bean定义,其实质是用该bean定义对应的类来创建真正实例的"配方(recipe)".把bean定义看成一个配方很有意义,它与cl ...
- Spring中FactoryBean的作用和实现原理
BeanFactory与FactoryBean,相信很多刚翻看Spring源码的同学跟我一样很好奇这俩货怎么长得这么像,分别都是干啥用的.BeanFactory是Spring中Bean工厂的顶层接口, ...
- Spring中ApplicationContextAware的作用
ApplicationContextAware 通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationContext方法. ...
- Spring中depends-on的作用是什么?
spring的IOC容器负责bean的管理,当实例化一个bean是,spring保证该Bean所依赖的其他bean已经初始化.一般情况下,用<ref>元素建立对其他bean的依赖关系. 比 ...
- Spring 中context.start作用
我们经常会看到 如下代码 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configPath. ...
- @Transacitonal注解不生效之spring中expose-proxy的作用与原理
几年前记得整理过,@Transacitonal注解的方法被另外一个方法调用的时候,事务是不生效的. 如果大量代码已经这么写了,这个时候抽取出去不现实,怎么办呢? 答案就是在<aop:aspect ...
- Spring中@Component的作用
今天在写程序的时候看见一个以前没有见过的注解(@Component),在网上查找过后,经过实践,决定把它记录下来. 1.@controller 控制器(注入服务) 用于标注控制层,相当于struts中 ...
- Spring中的TransactionProxyFactoryBean作用及配置(转)
问: 原文链接 http://blog.csdn.net/cpp_lzth/article/details/6551703 看AOP的时候发现spring中有个org.springframework. ...
- 【转】Spring中@Component的作用
今天在写程序的时候看见一个以前没有见过的注解(@Component),在网上查找过后,经过实践,决定把它记录下来. 1.@controller 控制器(注入服务) 用于标注控制层,相当于struts中 ...
随机推荐
- 鼠标放上去,div高度随文字增加,并显示剩余的文字。
/*这里是鼠标放上去显示全名 */ .kb2wText{display:block; height:20px; width:150px; line-height:20px; color:#0 ...
- Django - 02 优化一个应用
Django - 02 优化一个应用 上一篇中我们已经创建了一个blog app,现在来用一下~ 2.1 添加第一篇blog 这个post 列表很丑陋哦,连标题都木有显示~ 2.2 自定义bl ...
- hdu1081 To the Max
直接暴力枚举所有子矩形至少需要O(n^4)的复杂度,显然这不是一个合理的解决方法. 上述方案忽略了矩形之间的联系,进行了过多不必要的计算. 实际上如果固定矩形的左右边界,则底边在i行的矩形内数值之和与 ...
- 第十四章:高级I/O
14.1:引言 本章内容包括非阻塞I/O.记录锁.系统V流机制.I/O多路转接(select和poll函数).readv和writev函数以及存储映射I/O(mmap),这些都称为高级I/O. 14. ...
- html的<marquee></marquee>标签实现滚动效果
页面的自动滚动效果,可由javascript来实现,但是今天无意中发现了一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用m ...
- thinkphp 删除多条记录
删除id为123456的记录 $ids=array(1,2,3,4,5,6);$maps["id"] = array("in",$ids);$this-> ...
- WinMain函数详解(转载)
略加增添与修改! 工具:VC++6.0 系统:win7 64位 在Windows应用程序中,我们可以认为 WinMain() 函数是程序的入口,WinMain()的原型如下: int WI ...
- font awesome 符号字体
http://www.fontawesome.com.cn/ 引用CSS包之后根据图标库找到所需的图标代码 使用i标签或者a标签皆可,符号为文字性质,可以直接通过修改text颜色从而修改符号颜色
- ActiveMQ点对点的消息发送案例
公司最近会用MQ对某些业务进行处理,所以,这次我下载了apache-activemq-5.12.0-bin把玩下. 基于练习方便需要,使用Windows的版本. 参考的优秀文章: activemq的几 ...
- FZU 2168 防守阵地 I
Problem Description 部队中共有N个士兵,每个士兵有各自的能力指数Xi,在一次演练中,指挥部确定了M个需要防守的地点,按重要程度从低到高排序,依次以数字1到M标注每个地点的重要程度, ...