spring的log4j listener(webAppRootKey)
http://blog.csdn.net/pengchua/article/details/1943461
使用spring中的Log4jConfigListener有如如下好處:
?? 1. 動(dòng)態(tài)的改變記錄級(jí)別和策略,不需要重啟Web應(yīng)用,如《Effective Enterprise Java》所說(shuō)。
?? 2. 把log文件定在 /WEB-INF/logs/ 而不需要寫絕對(duì)路徑。
因?yàn)?系統(tǒng)把web目錄的路徑壓入一個(gè)叫webapp.root的系統(tǒng)變量。這樣寫log文件路徑時(shí)不用寫絕對(duì)路徑了.
log4j.appender.logfile.File=${webapp.root}/WEB-INF/logs/myfuse.log
?? 3. 可以把log4j.properties和其他properties一起放在/WEB-INF/ ,而不是Class-Path。
?? 4.log4jRefreshInterval為60000表示 開(kāi)一條watchdog線程每60秒掃描一下配置文件的變化;
?? 在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>60000</param-value>
??? </context-param>
??? <listener>
??????? <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
??? </listener>?
?
?
===========
<context-param>
??<param-name>log4jConfigLocation</param-name>
??<param-value>classpath:resource/properties/log4j.properties</param-value>
?</context-param>
<listener>
??<listener-class>
???org.springframework.web.util.Log4jConfigListener
??</listener-class>
?</listener>
=================http://blog.csdn.net/hwctl/article/details/1719597
web.xml中關(guān)于log4j的設(shè)置及范例說(shuō)明
分類: 小資料 2007-07-31 17:33 5984人閱讀 評(píng)論(1) 收藏 舉報(bào) 在web.xml有幾個(gè)條目和log4j有關(guān),它們是:?我們知道,在web-application中使用log4j,有很多配置方式:
a.
用servlet或者ServletContextListener對(duì)log4j做配置。通常,你不需要親自編寫servlet或者 listener,比如直接利用log4j的com.apache.jakarta.log4j.Log4jInit類,Spring的 org.springframework.web.util.Log4jConfigServlet和 org.springframework.web.util.Se
rvletContextListener,這種方式配置靈活,可以通過(guò)參數(shù)條目自行指定log4j.properties的位置。
b.
把log4j 的默認(rèn)配置文件(log4j.properties)放在classpath中,通常是/web-inf/classes目錄下.這種方式是log4j的 默認(rèn)配置方式,無(wú)須其他配置。缺點(diǎn)就是無(wú)法靈活的通過(guò)配置文件來(lái)指定log4j.properties的文件位置。在我們的petclinic項(xiàng)目中,因 為listener條目被注釋,所以采用的也是這種缺省方式。
現(xiàn)在我們考慮listener條目沒(méi)有被注釋的情況,這種情況和注冊(cè)Log4jConfigServlet的目的是一樣的,只是必須在支持listener的servlet?container中使用。
找 到.Log4jConfigServlet和ServletContextListener的源碼,他們都在適當(dāng)?shù)牡胤?(callback?method)調(diào)用了Log4jWebConfigurer.initLogging(getServletContext()); 定位到這個(gè)方法,第一句就是:WebUtils.setWebAppRootSystemProperty(servletContext);再定位到該 方法,方法很短:
從代碼看出,該方法其實(shí)就是把該web?application的根目錄的絕對(duì)文件路徑作為屬性保存在 System的屬性列表中。該屬性的名字,由web.xml文件中的名為"webAppRootKey"的參數(shù)值指出。如果不在web.xml中定義 webAppRootKey參數(shù),那么屬性名就是缺省的"webapp.root".在我們的petclinic項(xiàng)目中已經(jīng)定義了 webAppRootKey參數(shù),其值為"petclinic.root",因此,屬性的名字就是"petclinic.root".
再 回到Log4jWebConfigurer.initLogging(getServletContext()),接下來(lái)的行為是從web.xml中獲取 log4jConfigLocation和log4jRefreshInterval.前者指出log4j配置文件(有可能是xml格式)的位置和名字, 后者則指出重新都取配置文件的時(shí)間間隔.然后調(diào)用Log4jConfigurer.initLogging()方法對(duì)log4j做配置。從 Log4jConfigurer.initLogging()方法我們可以看到,針對(duì)不同格式的配置文件(properties或者xml), Log4jConfigurer采用不同的lo4j?configurator類做配置,例如DOMConfigurator或者 PropertyConfigurator。
至此,關(guān)于petclinic中關(guān)于log4j的配置,我們已經(jīng)基本上弄清楚了。可是System對(duì)象中的
petclinic.root屬性在什么時(shí)候使用呢?在web-inf/classes下面的log4j.properties文件中,有這么一句:
log4j.appender.logfile.File=${petclinic.root}/WEB-INF/petclinic.log
這 樣,我們就用上了petclinic.root屬性了。從上面的分析可知,如果你不在web.xml中定義webAppRootKey參數(shù),那么你就得把 log4j.appender.logfile.File=${petclinic.root}/WEB-INF/petclinic.log
中的petclinic.root變量改為webapp.root變量或者干脆換成別的絕對(duì)路徑了。?
?
總結(jié)
以上是生活随笔為你收集整理的spring的log4j listener(webAppRootKey)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: JDK1.5中的线程池(java.uti
- 下一篇: JAVA的内省机制(introspect