2014年3月29日 星期六

讓 Tomcat 獨立輸出各個 virtual host 的 catalina.out 檔案

以下測試環境為 RHEL5tomcat 6.0.39

一、swallowOutput設定

官網說明
If the value of this flag is true, the bytes output to System.out and System.err by the web application will be redirected to the web application logger. If not specified, the default value of the flag is false.

設定 tomcat/conf/context.cml
<Context swallowOutput="true">
……………………………………
</Context>

二、第一種方法,設定 log4j
1.      tomcat/lib/ 下建立 log4j.properties,以下為範例檔
##############################################################
#定義logger,名字這邊可自訂,第一個為輸出層級,為了讓log分開輸出,這邊只定義輸出層級
log4j.rootLogger=INFO, CATALINA
#定義每個appender的路徑與作用,名稱為剛定義的logger,參數請參考log4j官方文件
log4j.appender.CATALINA=org.apache.log4j.FileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.out
log4j.appender.CATALINA.Append=true
log4j.appender.CATALINA.Encoding=UTF-8
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

log4j.appender.S=org.apache.log4j.FileAppender
log4j.appender.S.File=${catalina.base}/logs/webapps1.out
log4j.appender.S.Append=true
log4j.appender.S.Encoding=UTF-8
log4j.appender.S.layout = org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

log4j.appender.R=org.apache.log4j.FileAppender
log4j.appender.R.File=${catalina.base}/logs/webapps2.out
log4j.appender.R.Append=true
log4j.appender.R.Encoding=UTF-8
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO, S
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[jacky.com]=INFO, R
#設定tocmat host綁定logger, hoststomcat server.xml host名稱,
#這邊為 localhost綁定CATALINA logger名稱, jacky.com 綁定 R logger名稱
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO,S
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[jacky.com]=INFO,R
###################################################################

2.      下載 log4j ,將log4j-1.2.17.jar 放到 tomcat/lib/

3.      下載 tomcat-juli.jar  tomcat-juli-adapters.jar放到 tomcat/lib/

4.      設定全域log4j
將剛下載的tomcat-juli.jar 取代 tomcat/bin/tomcat-juli.jar
5.      刪除 tomcat/conf/logging.properties 避免內建java.util.logging 產生log
6.      啟動tomcat


三、第二種方法,利用tomcat 內建logging.properties
1. 修改 tomcat/conf/logging.properties
####################################################################
#建立handlers,前綴名可自訂,但前面一定是數字開頭,
handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, 3jacky.org.apache.juli.FileHandler

.handlers = 1catalina.org.apache.juli.FileHandler

#設定每個handlers 層級與路徑
1catalina.org.apache.juli.FileHandler.level = FINE
1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.FileHandler.rotatable = false
1catalina.org.apache.juli.FileHandler.prefix = catalina
1catalina.org.apache.juli.FileHandler.suffix = .out

2localhost.org.apache.juli.FileHandler.level = FINE
2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.FileHandler.rotatable = false
2localhost.org.apache.juli.FileHandler.prefix = webapps1
2localhost.org.apache.juli.FileHandler.suffix = .out

3jacky.org.apache.juli.FileHandler.level = FINE
3jacky.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
3jacky.org.apache.juli.FileHandler.rotatable = false
3jacky.org.apache.juli.FileHandler.prefix = webapps2
3jacky.org.apache.juli.FileHandler.suffix = .out


#綁定handler tomcat host
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.FileHandler
org.apache.catalina.core.ContainerBase.[Catalina].[jacky.com].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[jacky.com].handlers = 3jacky.org.apache.juli.FileHandler
#############################################################################
2. tomcat啟動會讀tomcat/conf/logging.properties設定,如果檔案不可讀或未設定,則預設變為$JAVA_HOME/lib/logging.properties

3. tocmat 會 tomcat/bin/startup.sh 起動catalina.sh,所以java參數及變數要寫在tomcat/bin/catalina.sh,啟動才會生效

沒有留言:

張貼留言