使用cronolog 分割Tomcat日志 Apache日志
1. 安装cronolog
官网下载:http://cronolog.org/usage.html
./configure
make;make install
默认安装位置:
# which cronolog
/usr/local/sbin/cronolog
2. 配置分割Tomcat日志
编辑tomcat目录bin下的catalina.sh文件
# vi bin/catalina.sh
找到下面这行
elif [ "$1" = "start" ] ; then
……
org.apache.catalina.startup.Bootstrap "$@" start \
>> "$CATALINA_OUT" 2>&1 &
在这个elif语句中类似这样的行有2处,第一处是tomcat时带“-security”参数的启动,第二处是默认tomcat启动方式,也就是else下面的那部分,只修改这里就可以。为了都使用cronolog切割,我们两处都修改。
另外还要把touch “$CATALINA_OUT"这行注释掉。
完整的修改如下:
# touch "$CATALINA_OUT"
if [ "$1" = "-security" ] ; then
if [ $have_tty -eq 1 ]; then
echo "Using Security Manager"
fi
shift
"$_RUNJAVA" "$LOGGING_CONFIG" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
-Djava.security.manager \
-Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \
-Dcatalina.base="$CATALINA_BASE" \
-Dcatalina.home="$CATALINA_HOME" \
-Djava.io.tmpdir="$CATALINA_TMPDIR" \
org.apache.catalina.startup.Bootstrap "$@" start | /usr/local/sbin/cronolog /usr/local/tomcat/logs/catalina.%Y-%m-%d.out >> /dev/null &
# >> "$CATALINA_OUT" 2>&1 &
else
"$_RUNJAVA" "$LOGGING_CONFIG" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
-Dcatalina.base="$CATALINA_BASE" \
-Dcatalina.home="$CATALINA_HOME" \
-Djava.io.tmpdir="$CATALINA_TMPDIR" \
org.apache.catalina.startup.Bootstrap "$@" start 2>&1 | /usr/local/sbin/cronolog /usr/local/tomcat/logs/catalina.%Y%m%d.out >> /dev/null &
# >> "$CATALINA_OUT" 2>&1 &
fi
保存退出
重启Tomcat服务
[root@VM tomcat]# bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/jdk1.6.0_29
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar
[root@VM tomcat]# bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/jdk1.6.0_29
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar
查看日志目录是否生成catalina.yymmdd.out的日志文件
-rw-r--r-- 1 root root 10537 Jul 30 10:50 catalina.20140730.out
配置cronolog完成了,观察每天是否有一个新的catalina.yymmdd.out的日志文件生成,定期删除日期较旧的日志文件。
注意,当catalina.out日志有新内容时cronolog才会产生新日志文件,而不是定点生成。
3. 分割Apache日志
编辑apache的默认配置文件httpd.conf,找到Apache日志位置,修改如下(本例是按照每天分割):
ErrorLog "|/usr/local/sbin/cronolog /var/log/httpd/error_%Y-%m-%d.log"
CustomLog "|/usr/local/sbin/cronolog /var/log/httpd/access_%Y-%m-%d.log" combined
保存
重启apache服务
# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
注意:Apache的日志目录权限默认是700,只能生成error_xxxxxx.log日志,无法生产access_xxxxxx.log日志,我们需要给/var/log/httpd目录赋予other用户w权限
# chmod o+w /var/log/httpd
可以看见/var/log/httpd目录中生成了access_xxxxxx.log日志。
知识点:配置了cronolog,它并不会像定时那样在指定的时间到了就生成新日志,它生成日志的前提条件只有在日志中有了新内容后才会创建新日志文件,没有日志内容更新不会创建新日志。
3.1 测试
若想很快看到效果,可以按照下面的配置,周期设定为1分钟,新日志文件命名加上小时分钟
CustomLog "|/usr/local/sbin/cronolog --period=1minutes /var/log/httpd/access_%Y-%m-%d-%H%M.log" combined
重启Apache服务,使之生效
然后访问http页面,使新日志内容出现,只要有日志更新,就会每分钟生成一个新的access_xxxxxx.log文件,没有日志更新不会生成新日志。
-rw-r--r-- 1 root root 17280 Aug 2 01:10 access_2014-08-02-0100.log
-rw-r--r-- 1 root root 28620 Aug 2 01:13 access_2014-08-02-0110.log
-rw-r--r-- 1 root root 2160 Aug 2 01:18 access_2014-08-02-0115.log
附录
cronolog使用语法:
CustomLog "|/path/to/cronolog [OPTIONS] logfile-spec"[format]
常用options
--period=时间
单位必须指明,可用的单位包括 seconds, minutes, hours, days, weeks, months.
常用format
Specifier Description
%% a literal%character
%n a new-line character
%t a horizontal tab character
Time fields
%H hour (00..23)
%I hour (01..12)
%p the locale‘s AM or PM indicator
%M minute (00..59)
%S second (00..61, which allows for leap seconds)
%X the locale‘s time representation (e.g.: "15:12:47")
%Z time zone (e.g. GMT), or nothing if the time zone cannot be determined
Date fields
%a the locale‘s abbreviated weekday name (e.g.: Sun..Sat)
%A the locale‘s full weekday name (e.g.: Sunday .. Saturday)
%b the locale‘s abbreviated month name (e.g.: Jan .. Dec)
%B the locale‘s full month name, (e.g.: January .. December)
%c the locale‘s date and time (e.g.:"Sun Dec 15 14:12:47 GMT 1996")
%d day of month (01 .. 31)
%j day of year (001 .. 366)
%m month (01 .. 12)
%U week of the year with Sunday as first day of week (00..53, where week 1 is the week containing the first Sunday of the year)
%W week of the year with Monday as first day of week (00..53, where week 1 is the week containing the first Monday of the year)
%w day of week (0 .. 6, where 0 corresponds to Sunday)
%x locale‘s date representation (e.g. today in Britain: "15/12/96")
%y year without the century (00 .. 99)
%Y year with the century (1970 .. 2038)
本文出自 “光普-IT公社” 博客,请务必保留此出处http://guangpu.blog.51cto.com/3002132/1566433
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。