tomcat 使用log4j進行日誌分割
由於tomcat catalina.out日誌不會自動分割,
一、日誌分割所需包在附近中
1、 壓縮包中有三個jar包:
log4j-1.2.16.jar
tomcat-juli-adapters.jar
tomcat-juli.jar
說明:如果不是tomcat7版本,請去官網下載對應版本的tomcat-juli.jar,tomcat-juli-adapters.jar類包,
下載地址: http://www.apache.org/dist/tomcat 在對應版本中的 bin/extras路徑下
二、修改相關配置檔案
1、 修改${catalina.home}/conf/context.xml
修改:<Context >為<Context swallowOutput="true" >只有這樣才能完全的把tomcat的stdout給接管過來。
2、 複製log4j-1.2.16.jar和tomcat-juli-adapters.jar 到tomcat的lib下
3、 複製tomcat-juli.jar到${catalina.home}/bin下,在該目錄會存在該檔案,覆蓋即可
4、 建立log4j.properties,並把其放到到tomcat的/lib下
Log4j.properties的內容如下:相關引數根據需要可以相應修改
log4j.rootLogger=INFO, CATALINA
# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File=/home/richmail/logs/tomcat/catalina.log
log4j.appender.CATALINA.Append=true
log4j.appender.CATALINA.Encoding=UTF-8
# Roll-over the log once per day
log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.LOCALHOST=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOCALHOST.File=/home/richmail/logs/tomcat/localhost.log
log4j.appender.LOCALHOST.Append=true
log4j.appender.LOCALHOST.Encoding=UTF-8
log4j.appender.LOCALHOST.DatePattern='.'yyyy-MM-dd
log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MANAGER.File=/home/richmail/logs/tomcat/manager.log
log4j.appender.MANAGER.Append=true
log4j.appender.MANAGER.Encoding=UTF-8
log4j.appender.MANAGER.DatePattern='.'yyyy-MM-dd
log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.HOST-MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.HOST-MANAGER.File=/home/richmail/logs/tomcat/host-manager.log
log4j.appender.HOST-MANAGER.Append=true
log4j.appender.HOST-MANAGER.Encoding=UTF-8
log4j.appender.HOST-MANAGER.DatePattern='.'yyyy-MM-dd
log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Encoding=UTF-8
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
# Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]=INFO, MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]=INFO, HOST-MANAGER
5. 修改bin/catalina.sh
a. 遮蔽或刪除如下內容:
(1) if [ -z "$CATALINA_OUT" ] ; then
CATALINA_OUT=$CATALINA_BASE/logs/catalina.out
fi(2) touch "$CATALINA_OUT"
(3) >> "$CATALINA_OUT" 2>&1 "&" (兩處)
b. 修改org.apache.catalina.startup.Bootstrap "[email protected]" start \ 內容 (兩處)
修改為:org.apache.catalina.startup.Bootstrap "[email protected]" start & \
6. 修改conf/server.xml中localhost_access_log日誌路徑
<Valve className="org.apache.catalina.valves.AccessLogValve" directory=" /home/richmail/logs/tomcat "
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
7. 刪除 conf/logging.properties 檔案
8. 啟動tomcat。