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中 ...
随机推荐
- 使用Window Live Writer写博客
1.打开“日志账户”—>“日志选项”. 2.点击“更新账户信息”. 3.输入博客地址,用户名和密码,点击“下一步”. 4.耐心等待片刻... 5.设置“日志昵称”,点击“完成”. 这样就大功告成 ...
- 山东理工大学第七届ACM校赛-完美素数 分类: 比赛 2015-06-26 10:36 15人阅读 评论(0) 收藏
完美素数 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 我们定义:如果一个数为素数,且这个数中含有7或3,那么我们称这个数为完美 ...
- Poj(1274),二分图匹配
题目链接:http://poj.org/problem?id=1274 The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Tota ...
- python两个dataframe的合并
见http://pandas.pydata.org/pandas-docs/stable/merging.html
- Android中直播视频技术探究之---采集摄像头Camera视频源数据进行推流(采用金山云SDK)
一.前言 在之前已经详细介绍了Android中的一种视频数据源:Camera,不了解的同学可以点击进入:Android中Camera使用详解 ,在这篇文章中我们介绍了如何采集摄像头的每一帧数据,然后进 ...
- 【Java】hashcode()和equals()
大家知道,在集合中判断集合中的两个元素是否相同,依赖的是hashcode()和equals()两个方法. > 一个简单的实验 public class Teacher { private Int ...
- C语言中的结构体,结构体数组
C语言中的结构体是一个小难点,下面我们详细来讲一下:至于什么是结构体,结构体为什么会产生,我就不说了,原因很简单,但是要注意到是结构体也是连续存储的,但要注意的是结构体里面类型各异,所以必然会产生内存 ...
- 使用Windows安装的最高版本IE内核加载内嵌页(转载)
客户端程序内嵌Webbrowser控件时,默认情况都是使用IE7兼容模式打开网页的.但是IE7有很多新的特性不支持,导致无法正常显示出来,所以需要强制使用高版本的IE内核来加载.渲染. void Ch ...
- Duilib实现 Windows资源管理器前进后退原理
一.用两个vector保存路径 vector<wstring> vctBacks;//保存后退路径的集合 vector<wstring> vctForwards//保存前进路径 ...
- Struts2的类型转换
Struts2的类型转换 类型转换的作用: HTTP请求都是字符串类型,需要把这些字符串类型转化成相应的数据类型,使得Web应用的控制组件可以使用. 1.內建的类型转换器 Struts2可以完成大多数 ...