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 添加

[html] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. <context-param>   
  2.       <param-name>log4jConfigLocation</param-name>   
  3.       <param-value>WEB-INF/log4j.properties</param-value>   
  4.   </context-param>   
  5.   
  6.   <context-param>   
  7.       <param-name>log4jRefreshInterval</param-name>   
  8.       <param-value>6000</param-value>   
  9.   </context-param>   
  10.   
  11.   <listener>   
  12.       <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>   
  13.   </listener>  


在使用spring先后開發了兩個模塊,單獨測試都正常。也先后上線運行,之后發現有個模塊在啟動Tomcat后總是初始化失敗,必須到tomcat管理控制台手動啟動。找了半天也沒發現原因。后來管理員在每次重啟Tomcat后這個模塊沒有運行導致一堆問題和麻煩,今天特意查看了其他的tomcat日志文件,終於發現了問題所在,原來是Log4jConfigListener。使用它是為了隨時調整打印日志的級別而不用重啟服務。沒想到沒有享受到它的便利,反而出了一堆問題,只能怪自己沒有稍微仔細研究一下。 

[html] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. <context-param>     
  2.     <param-name>webAppRootKey</param-name>     
  3.         <param-value>cang.qing6.com.root</param-value>     
  4.     </context-param>     
  5.      
  6.     <context-param>     
  7.         <param-name>log4jConfigLocation</param-name>     
  8.         <param-value>/WEB-INF/classes/log4j.properties</param-value>     
  9.     </context-param>     
  10.      
  11.     <context-param>     
  12.         <param-name>log4jRefreshInterval</param-name>     
  13.         <param-value>6000</param-value>     
  14.     </context-param>     
  15.     <listener>     
  16.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>     
  17.     </listener>     
  18.   
  19. <context-param>  
  20.  <param-name>webAppRootKey</param-name>  
  21.   <param-value>cang.qing6.com.root</param-value>  
  22.  </context-param>  
  23.   
  24.  <context-param>  
  25.   <param-name>log4jConfigLocation</param-name>  
  26.   <param-value>classpath:log4j.properties</param-value>  
  27.  </context-param>  
  28.   
  29.  <context-param>  
  30.   <param-name>log4jRefreshInterval</param-name>  
  31.   <param-value>6000</param-value>  
  32.  </context-param>  
  33.  <listener>  
  34.   <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  35.  </listener>  


log4j.properties配置

[html] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. layoutPattern=[%d{HH:mm:ss}] %-5p : %m%n      
  2. log.file=${message.web.root}/logs/app.log      
  3.      
  4. log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender      
  5. log4j.appender.logfile.File=${log.file}      
  6. log4j.appender.logfile.Append=true     
  7. log4j.appender.logfile.DatePattern='.'yyyyMMdd      
  8. log4j.appender.logfile.layout=org.apache.log4j.PatternLayout      
  9. log4j.appender.logfile.layout.ConversionPattern=${layoutPattern}     
  10.   
  11. layoutPattern=[%d{HH:mm:ss}] %-5p : %m%n  
  12. log.file=${message.web.root}/logs/app.log  
  13.   
  14. log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender  
  15. log4j.appender.logfile.File=${log.file}  
  16. log4j.appender.logfile.Append=true  
  17. log4j.appender.logfile.DatePattern='.'yyyyMMdd  
  18. log4j.appender.logfile.layout=org.apache.log4j.PatternLayout  
  19. log4j.appender.logfile.layout.ConversionPattern=${layoutPattern}  

其實需要注意的地方就是應用服務器下有不止一個的應用在使用spring的Log4jConfigListener需要修改web環境中webAppRootKey值(這個值其實是web應用的根目錄在環境變量名,這樣在log4j配置文件中如果有相對web目錄的路徑就不用寫死了)。 
否則兩個默認值web.root在環境變量中就會有沖突導致第二個應用啟動失敗。

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

近日,因為懶惰,直接從原有項目切出一個分塊成了一個項目,然后同時發布啟動,出現以下異常

[java] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. Exception sending context initialized event to listener instance of class org.springframework.web.util.Log4jConfigListener  
  2. 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!  
  3.   
  4. at org.springframework.web.util.WebUtils.setWebAppRootSystemProperty(WebUtils.java:99)  
  5.     at org.springframework.web.util.Log4jWebConfigurer.initLogging(Log4jWebConfigurer.java:116)  
  6.     at org.springframework.web.util.Log4jConfigListener.contextInitialized(Log4jConfigListener.java:51)  
  7.     at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3773)  
  8.     at org.apache.catalina.core.StandardContext.start(StandardContext.java:4270)  
  9.     at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:866)  
  10.     at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:850)  
  11.     at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:638)  
  12.     at org.apache.catalina.core.StandardHostDeployer.install(StandardHostDeployer.java:320)  
  13.     at org.apache.catalina.core.StandardHost.install(StandardHost.java:875)  
  14.     at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:657)  
  15.     at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:476)  
  16.     at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1008)  
  17.     at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:394)  
  18.     at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:166)  
  19.     at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1134)  
  20.     at org.apache.catalina.core.StandardHost.start(StandardHost.java:832)  
  21.     at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1126)  
  22.     at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:521)  
  23.     at org.apache.catalina.core.StandardService.start(StandardService.java:519)  
  24.     at org.apache.catalina.core.StandardServer.start(StandardServer.java:2345)  
  25.     at org.apache.catalina.startup.Catalina.start(Catalina.java:594)  
  26.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  27.     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)  
  28.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)  
  29.     at java.lang.reflect.Method.invoke(Method.java:324)  
  30.     at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:297)  
  31.     at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:398)  


看看異常,還是挺簡單的,應該是兩個項目的設置重復了,導致出錯,但我發現web.xml里並沒有配置webAppRootKey項,原來是因為如果沒有web.xm 內沒有設置webAppRootKey項,是為默認設置

[java] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. public  static  void  setWebAppRootSystemProperty(ServletContext servletContext) throws  IllegalStateException  {  
  2.         String  param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);  
  3.         String  key = (param != null  ? param : DEFAULT_WEB_APP_ROOT_KEY);  
  4.         String  oldValue = System .getProperty(key);  
  5.         if  (oldValue != null ) {  
  6.             throw  new  IllegalStateException ("WARNING: Web app root system property already set: "  + key + " = "  +  
  7.                                                                   
  8.             oldValue + " - Choose unique webAppRootKey values in your web.xml files!" );  
  9.         }  
  10.         String  root = servletContext.getRealPath("/" );  
  11.         if  (root == null ) {  
  12.             throw  new  IllegalStateException ("Cannot set web app root system property when WAR file is not   
  13. expanded");  
  14.         }  
  15.         System .setProperty(key, root);  
  16.         servletContext.log("Set web app root system property: "  + key + " = "  + root);  
  17.     }  


從代碼看出,該方法其實就是把該web application的根目錄的絕對文件路徑作為屬性保存在 System的屬性列表中。該屬性的名字,由web.xml文件中的名為"webAppRootKey"的參數值指出。如果不在web.xml中定義 webAppRootKey參數,那么屬性名就是缺省的"webapp.root".在我們的petclinic項目中已經定義了 webAppRootKey參數,其值為"petclinic.root",因此,屬性的名字就是"petclinic.root".



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM