最近遇到一个Tomcat日志catalina.out日志文件过大的问题,现总结如下:本人用第二种方法解决问题,本人认为第二种更简单容易操作!
1、下载cronolog工具,我下载的版本是cronolog-1.6.2
(yum安装:yum install cronolog)
2、将下载好的文件解压,tar xvzf cronolog-1.6.2.tar.gz
3、切换到解压后的文件目录下:cd cronolog-1.6.2
4、初始化和编译安装
./configure --prefix=/usr/local/cronolog
#make
#make install
#ln -s /usr/local/cronolog/sbin/* /usr/local/sbin/
5、查看安装版本
#cronolog --version
6、修改tomcat的启动文件(tomcat目录/bin/catalina.sh)
vi catalina.sh
(1)修改输出日志路径
修改:
if [ -z "$CATALINA_OUT" ] ; then
CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out
fi
为:
if [ -z "$CATALINA_OUT" ] ; then
CATALINA_OUT="$CATALINA_BASE"/logs/catalina.%Y-%m-%d.out
fi
(2)删除生成日志文件
注释:
touch "$CATALINA_OUT"
为:
#touch "$CATALINA_OUT"
(3)修改启动脚本参数(两项)
修改:
org.apache.catalina.startup.Bootstrap "$@" start \
>> "$CATALINA_OUT" 2>&1 "&"
为:
org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \
| /usr/local/sbin/cronolog "$CATALINA_OUT" >> /dev/null &
7、重启tomcat
tomcat输出日志文件分割成功,输出log文件格式为:catalina.2015-06-30.out
1.首先编辑logrotate.conf文件,打开compress选项(去掉注释)
[root@localhost ~]# cat /etc/logrotate.conf | grep -v "^$"| grep -v "#"
weekly
rotate 4
create
dateext
compress <<这一项
2.添加指定文件,在/etc/logrotate.d/目录下新建一个名为tomcat的文件
[root@localhost ~]# cat > /etc/logrotate.d/tomcat <
/home/tomcat/utr/logs/catalina.out{ #要轮转的文件
copytruncate # 创建新的catalina.out副本后,截断源catalina.out文件
daily # 每天进行catalina.out文件的轮转
rotate 7 # 至多保留7个副本
missingok # 如果要轮转的文件丢失了,继续轮转而不报错
compress # 使用压缩的方式(节省硬盘空间;一个2~3GB的日志文件可以压缩成60MB左右)
size 16M # 当catalina.out文件大于16MB时,就轮转
}
EOF
参数说明:
copytruncate #备份日志并截断源文件
nocopytruncate # 备份日志文件不截断
dateext #使用当期日期作为命名格式
notifempty #当日志文件为空时,不进行轮转
daily # 每天进行文件的轮转
size 16M # 当文件大于16MB时,就会轮转
rotate 30 #指定日志文件删除之前转储的次数
3.执行方式
①自动执行原理
1.每天晚上crond守护进程会运行在/etc/cron.daily目录中的任务列表;
2.与logrotate相关的脚本也在/etc/cron.daily目录中。运行的方式为"/usr/bin/logrotate /etc/logrotate.conf";
3./etc/logrotate.conf文件include了/etc/logrotate.d/目录下的所有文件。还包括我们上面刚创建的tomcat文件;
4./etc/logrotate.d/tomcat文件会触发/usr/local/apache-tomcat-8.0.28/logs/catalina.out文件的轮转。
②手动执行:logrotate /etc/logrotate.conf
③只轮转刚刚的tomcat配置文件:logrotate --force /etc/logrotate.d/tomcat
log4j.rootLogger=INFO, CATALINA
log4j.logger.org.apache=INFO, CATALINA
log4j.logger.org.[hibernate](http://lib.csdn.net/base/javaee)=WARN, CATALINA
log4j.logger.org.springframework=WARN, CATALINA
# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.out
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’.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.
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.
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.
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
# 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