Jboss遷移到Tomcat之手記(二)- Log4J

TomcatJboss有個很重要的區別,Jboss是會把$JBOSS_HOME/server/all/deploy/project.war包含在CLASSPATH裏,而Tomcat不會,只有$PROJECT.WAR/WEB-INF/classes裏。因此java代碼ClassA.class.getResourceAsStream(「WEB-INF/a.properties」)沒法讀取到文件,由於classpath沒包含此項目主目錄。對應log4j,就是不要在project.warlog4j.properties/log4j.xml來配置log4J,而應該在project.war/WEB-INF/classes下。Java代碼讀取這裏的代碼能夠加.JVM啓動shellclasspath,或者用Listener來設置變量表示應用主目錄:以下是spring的一個Listener能夠配置系統變量projeta.root, java代碼能夠在web應用中(必須在web應用中,若是非web應用,能夠容易的加classpath來解決)用System.getProperty(「projecta.root」)來讀取應用的物理主目錄:
<context-param>
                   <param-name>webAppRootKey</param-name>
                   <param-value>projecta.root</param-value>
         </context-param>
         <listener>
                   <listener-class>
                            org.springframework.web.util.WebAppRootListener
                   </listener-class>
         </listener>
 
Tomcat內建的是jdklogging來跟蹤日誌,配置在$TOMCAT_HOME/conf/logging.properties.
1catalina.org.apache.juli.FileHandler.level = FINE
1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.FileHandler.prefix = catalina.
java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers=1catalina.org.apache.juli.FileHandler
 
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/projecta].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers=1catalina.org.apache.juli.FileHandler
 
若是要Tomcat(不是僅僅某個app)改用Log4j,根據Tomcat官方網站介紹,須要用Juli-Adapters來實現(http://tomcat.apache.org/tomcat-7.0-doc/logging.html)。
Tomcat編譯獲得(Ant 1.8.2:(http://ant.apache.org/bindownload.cgi)Tomcat 7 Source:(http://tomcat.apache.org/download-70.cgi) ant extras)的extras目錄下的tomcat-juli.jar覆蓋$TOMCAT_HOME/bin裏,tomcat-juli-adapters.jar複製到$TOMCAT_HOME/lib裏, log4j.jarcommon-logging.jar$TOMCAT_HOME/lib,在$TOMCAT_HOME/lib放置log4j.properties/log4j.xml便可實現log4j來記錄程序日誌了。
tomcat-juli.jar,tomcat-juli-adapters.jar(extras)
tomcat-juli.jar to override tomcat.home/bin.
tomcat-juli-adapters.jar to tomcat.home/lib.
log4j to tomcat.home/lib
log4j.properties/log4j.xml to tomcat.home/lib
mv conf/logging.properties conf/logging.properties.bak
若是隻是某個web application須要用log4jtomcat自己還延用JDK logging的話,只須要log4j/common-logging/log4j配置在CLASSPATH裏能找到便可實現。若是log4j配置不止一個,會層疊效果,若是重複項,會後者覆蓋前者(classpath順序而定)。
相關文章
相關標籤/搜索