Tomcat跟Jboss有個很重要的區別,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.war放log4j.properties/log4j.xml來配置log4J,而應該在project.war/WEB-INF/classes下。Java代碼讀取這裏的代碼能夠加.到JVM啓動shell的classpath,或者用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內建的是jdk的logging來跟蹤日誌,配置在$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-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須要用log4j而tomcat自己還延用JDK logging的話,只須要log4j/common-logging/log4j配置在CLASSPATH裏能找到便可實現。若是log4j配置不止一個,會層疊效果,若是重複項,會後者覆蓋前者(classpath順序而定)。