先吐嘈一下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