首页 > 代码库 > tomcat 8.5.9.0 解决catalina.out过大的问题
tomcat 8.5.9.0 解决catalina.out过大的问题
先吐嘈一下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 appenderslog4j.appender.CATALINA=org.apache.log4j.RollingFileAppenderlog4j.appender.CATALINA.File=${catalina.base}/logs/catalina.loglog4j.appender.CATALINA.MaxFileSize=10240KBlog4j.appender.CATALINA.MaxBackupIndex=100log4j.appender.CATALINA.Append=truelog4j.appender.CATALINA.Encoding=UTF-8log4j.appender.CATALINA.DatePattern=‘.‘yyyy-MM-dd-HH-mm‘.log‘log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayoutlog4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%nlog4j.appender.LOCALHOST=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.LOCALHOST.File=${catalina.base}/logs/localhost.loglog4j.appender.LOCALHOST.Append=truelog4j.appender.LOCALHOST.Encoding=UTF-8log4j.appender.LOCALHOST.DatePattern=‘.‘yyyy-MM-dd‘.log‘log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayoutlog4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%nlog4j.appender.MANAGER=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.MANAGER.File=${catalina.base}/logs/manager.loglog4j.appender.MANAGER.Append=truelog4j.appender.MANAGER.Encoding=UTF-8log4j.appender.MANAGER.DatePattern=‘.‘yyyy-MM-dd‘.log‘log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayoutlog4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%nlog4j.appender.HOST-MANAGER=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.HOST-MANAGER.File=${catalina.base}/logs/host-manager.loglog4j.appender.HOST-MANAGER.Append=truelog4j.appender.HOST-MANAGER.Encoding=UTF-8log4j.appender.HOST-MANAGER.DatePattern=‘.‘yyyy-MM-dd‘.log‘log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayoutlog4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%nlog4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderlog4j.appender.CONSOLE.Encoding=UTF-8log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayoutlog4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n# Configure which loggers log to which appenderslog4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=WARN, LOCALHOSTlog4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]=WARN, MANAGERlog4j.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、kill掉tomcat进程,重新启动
顺利的话,logs/目录下的catalina.out文件应该不会再生成了,但是实际发现,有时这样仍然不起作用,需要再下狠招。
7、修改{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
tomcat 8.5.9.0 解决catalina.out过大的问题