javascript
Spring MVC中 log4j日志文件配置相对路径
log4j和web.xml配置webAppRootKey 的問題
?
1?在web.xml配置?
<context-param>
??<param-name>webAppRootKey</param-name>
??<param-value>web.sample.root</param-value>
</context-param>
可以用System.getProperty("web.sample.root")來獲取屬性值。在Eclipse調試Web項目時,項目的路徑是一個臨時路徑,不在真正的路徑下,可以通過上述語句打印出屬性值,來看看臨時項目路徑在哪里
如:System.out.println("web.root:"+ System.getProperty("web.root"));
????輸出:web.root:D:\apache-tomcat-6.0.30\webapps\wangzun\
2、Spring通過 org.springframework.web.util.WebAppRootListener 這個監聽器來壓入項目路徑。但是如果在web.xml中已經配置了 org.springframework.web.util.Log4jConfigListener 這個監聽器,則不需要配置WebAppRootListener了。因為Log4jConfigListener已經包含了WebAppRootListener的功能.
配置WebAppRootListener:
?<listener>
??<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
?</listener>
3、部署在同一容器中的多個Web項目,要配置不同的<param-value>,不能重復webAppRootKey的系統變量名
4.WebAppRootListener要在ApplicationContext的ContextLoaderListener之前,否則ApplicationContext的bean注入根目錄值時會發生無法注入異常。
???<!-- 項目根目錄Listener -->
?<listener>
??<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
?</listener>
?<!--Spring的ApplicationContext 載入 -->
?<listener>
??<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
?</listener>
4、如果配置了
log4j.appender.file.File=${web.sample.root}WEB-INF/logs/sample.log??
log4j會自己自動建立logs目錄, 不需要手工顯式建立空的logs目錄
在tomcat下部署兩個或多個項目時,web.xml文件中最好定義webAppRootKey參數,如果不定義,將會缺省為“webapp.root”,如下:
<!-- 應用路徑 -->?
<context-param>?
<param-name>webAppRootKey</param-name>?
<param-value>webapp.root</param-value>?
</context-param>?
最好報紙每個項目的參數值不同,以免引起項目沖突
嚴重: 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' = [C:\Program Files (x86)\Apache Software Foundation\Tomcat 6.0\webapps\DRMProject\] instead of [C:\Program Files (x86)\Apache Software Foundation\Tomcat 6.0\webapps\DRMSn\] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!
對多個項目要對webAppRootKey進行配置,這里主要是讓log能將日志寫到對應項目根目錄下,如我配置這兩個項目的webAppRootKey為
項目1 的 web.xml:
<!-- 應用路徑 -->
<context-param>?
<param-name>webAppRootKey</param-name>?
<param-value>webapp.root1</param-value>?
</context-param>
?<!-- 項目根目錄Listener -->
?<listener>
??<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
?</listener>
項目2的 web.xml:
<!-- 應用路徑 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root2</param-value>
</context-param>
?<!-- 項目根目錄Listener -->
?<listener>
??<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
?</listener>
這樣就不會出現沖突了。
定義以后,在Web Container啟動時將把ROOT的絕對路徑寫到系統變量里。
然后log4j的配置文件里就可以用${webName.root }來表示Web目錄的絕對路徑,把log文件存放于webapp中。
namemax:
?<context-param>
??<param-name>webAppRootKey</param-name>
??<param-value>web.root</param-value>
?</context-param>
?<!-- 項目根目錄Listener -->
?<listener>
??<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
?</listener>
bean中可以使用:
?? <bean id="transformChinese" class="com.zunmi.util.TransformChinese"
?????? ??p:outBasePath="${web.root}WEB-INF/destineDomainFile/"
???????? p:j2fSource="${web.root}WEB-INF/SimpleToTraditional.properties"?
???????? p:charSet="gbk"
??????? />
?
總結
以上是生活随笔為你收集整理的Spring MVC中 log4j日志文件配置相对路径的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringMVC 使用hibernat
- 下一篇: INFO org.apache.hado