1. 程式人生 > >解決catalina.out過大的問題

解決catalina.out過大的問題

catalina.out tomcat 日誌過大 log

先吐嘈一下tomcat這個項目,日誌切割這麽常見的功能,tomcat這種知名開源項目默認居然不開啟,生產環境跑不了幾天,磁盤就滿了,而且很多網上流傳的方法,比如修改conf/logging.properties文件,將級別設置成OFF,我試了好象並不管用(tomcat 8.5.9.0版本 + centos 6.5 + jdk1.8環境)

正確姿勢:

1、下載 https://mirrors.tuna.tsinghua.edu.cn/apache/logging/log4j/1.2.17/log4j-1.2.17.tar.gz,解壓後,將其中的apache-log4j-1.2.17/log4j-1.2.17.jar 文件復制到{tomcat_home}/lib目錄

2、下載http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.0.39/bin/extras/tomcat-juli-adapters.jar 到{tomcat_home}/lib目錄

3、在{tomcat_home}/lib下創建log4j.properties文件,內容參考下面這樣:
log4j.rootLogger=WARN, CATALINA

#Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.RollingFileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.log

log4j.appender.CATALINA.MaxFileSize=10240KB
log4j.appender.CATALINA.MaxBackupIndex=100
log4j.appender.CATALINA.Append=true
log4j.appender.CATALINA.Encoding=UTF-8
log4j.appender.CATALINA.DatePattern=‘.‘yyyy-MM-dd-HH-mm‘.log‘
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=${catalina.base}/logs/localhost.log
log4j.appender.LOCALHOST.Append=true
log4j.appender.LOCALHOST.Encoding=UTF-8
log4j.appender.LOCALHOST.DatePattern=‘.‘yyyy-MM-dd‘.log‘
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=${catalina.base}/logs/manager.log
log4j.appender.MANAGER.Append=true
log4j.appender.MANAGER.Encoding=UTF-8
log4j.appender.MANAGER.DatePattern=‘.‘yyyy-MM-dd‘.log‘
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=${catalina.base}/logs/host-manager.log
log4j.appender.HOST-MANAGER.Append=true
log4j.appender.HOST-MANAGER.Encoding=UTF-8
log4j.appender.HOST-MANAGER.DatePattern=‘.‘yyyy-MM-dd‘.log‘
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]=WARN, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]=WARN, MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]=WARN, HOST-MANAGER
4、下載http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.0.39/bin/extras/tomcat-juli.jar,用這個jar替換掉{tomcat_home}/bin下的同名文件

5、刪除{tomcat_home}/conf/logging.properties 文件

6、 刪除{tomcat_home}\logs\的歷史日誌(主要是catalina.out這個文件)

7、kill掉tomcat進程,重新啟動

順利的話,logs/目錄下的catalina.out文件應該不會再生成了,但是實際發現,有時這樣仍然不起作用,需要再下狠招。

8、修改{tomcat_home}/bin/catalina.sh ,找到下面的位置

if [ -z "$CATALINA_OUT" ] ; then
#CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out
CATALINA_OUT=/dev/null
fi
大概在207行,將CATALINA_OUT的目錄修改為/dev/null這個空設備,然後再重啟tomcat

解決catalina.out過大的問題