本文在博主http://freeloda.blog.51cto.com/2033581/1346076基础上增加了新的内容,方便大家学习和阅读。      

cronolog 简介及特点  

      apache服务器,默认日志文件是不分割的,一个整文件既不易于管理,也不易于分析统计。cronolog 是一个简单的过滤程序,读取日志文件条目从标准输入和输出的每个条目并写入指定的日志文件的文件名模板和当前的日期和时间。分割访问日志为每天或每月的日志。

       cronolog主要和Web服务器配置使用,Apache 默认日志文件是不分割的,一个整文件既不易于管理,也不易于分析统计。安装cronolog后,可以将日志文件按时间分割,易于管理和分析。下面是与Apache配置的一些指令:

1
2
TransferLog "|/usr/sbin/cronolog /web/logs/%Y/%m/%d/access.log"
ErrorLog     "|/usr/sbin/cronolog /web/logs/%Y/%m/%d/errors.log"

具体案例,

1
2
/web/logs/2002/12/31/access .log /web/logs/2002/12/31/errors .log
/web/logs/2003/01/01/access .log /web/logs/2003/01/01/errors .log

cronolog 安装

软件下载http://cronolog.org/download/index.html

1.安装yum源


[root@node6 src] # wget http://ftp.sjtu.edu.cn/fedora/epel/6/i386/epel-release-6-8.noarch.rpm
[root@node6 src] # rpm -ivh epel-release-6-8.noarch.rpm
warning: epel-release-6-8.noarch.rpm: Header V3 RSA /SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                 ########################################### [100%]
1:epel-release           ########################################### [100%]

2.安装ntp

1
[root@node6 src] # yum install -y ntp

3.时间同步

1
2
[root@node6 src] # ntpdate 202.120.2.101
28 Dec 17:59:17 ntpdate[1413]: step time server 202.120.2.101 offset -25666.776448 sec

4.安装cronolog

(1).用yum安装

1
[root@node6 src] # yum install -y cronolog httpd

(2).源码安装

1
2
3
4
5
6
7
[root@node6 src] # wget http://cronolog.org/download/cronolog-1.6.2.tar.gz
[root@node6 src] # tar xf cronolog-1.6.2.tar.gz
[root@node6 src] # cd cronolog-1.6.2
[root@node6 cronolog-1.6.2] # ./configure
[root@node6 cronolog-1.6.2] # make && make install
[root@localhost ~] # which cronolog
/usr/local/sbin/cronolog

cronolog 使用

(1).基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@node6 ~] # cronolog -h
usage: cronolog [OPTIONS] logfile-spec
-H NAME,   --hardlink=NAME maintain a hard link from NAME to current log
-S NAME,   -- symlink =NAME  maintain a symbolic link from NAME to current log
-P NAME,   --prev- symlink =NAME  maintain a symbolic link from NAME to previous log
-l NAME,   --link=NAME     same as -S /--symlink
-h,        --help          print this help, then exit
-p PERIOD, --period=PERIOD set the rotation period explicitly
-d DELAY,  --delay=DELAY   set the rotation period delay
-o,        --once-only     create single output log from template (not rotated)
-x FILE,   --debug=FILE    write debug messages to FILE
( or to standard error if FILE is "-" )
-a,        --american         American date formats
-e,        --european         European date formats (default)
-s,    --start- time =TIME   starting time
-z TZ, -- time -zone=TZ      use TZ for timezone
-V,      --version         print version number, then exit

cronolog 一般采取管道的方式来工作的,采用如下的形式:

1
[root@node6 ~] # loggenerator | cronolog log_file_pattern

其中,loggenerator为产生log的程序,而log_file_pattern是日志文件的路径,可以在其中加入cronolog所支持的时间相关的pattern字符,如/www/log/%y/%m/%d/access.log。其pattern为%字符后跟特殊字符,简述如下:

转义符:  

1
2
3
%    %字符
n    换行
t    水平制表符

时间域:  

1
2
3
4
5
6
7
H    小时(00..23)
I    小时(01..12)
p    该locale下的AM或PM标识
M    分钟(00..59)
S    秒 (00..61, which allows for leap seconds)
X    该locale下时间表示符(e.g.: "15:12:47" )
Z    时区。若时区不能确定,则无意义

日期域:  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
a    该locale下的工作日简名(e.g.: Sun..Sat)
A    该locale下的工作日全名(e.g.: Sunday ..  Satur-ay)
b    该locale下的月份简称(e.g.: Jan .. Dec)
B    该locale下的月份全称(e.g.:  January .. December)
c    该locale下的日期和时间(e.g.: "Sun Dec 15  14:12:47 GMT 1996" )
d    当月中的天数 (01 .. 31)
j    当年中的天数 (001 .. 366)
m    月数 (01 .. 12)
U    当年中的星期数,以周日作为一周开始,其中第一周为首个含星期天的星期(00..53)
W    当年中的星期数,以星期一作为一周的开始,其中第一周为首个含星期天的星期(00..53)
w    工作日数(0 .. 6, 0表示星期天)
x    该locale下的日期表示(e.g. "13/04/97" )
y    两位数的年份(00 .. 99)
Y    四位数的年份(1970 .. 2038)

(2).结合apache使用

编辑httpd.conf文件,将其中的

1
2
3
4
[root@localhost ~] # vim /usr/local/apache2/conf/httpd.conf
将默认日志: CustomLog "logs/access_log" combined
修改为:CustomLog "|/usr/local/sbin/cronolog /log/www/access_%Y%m%d.log" combined 即可。其中%Y%m%d为日志文件分割方式,即为“年月日”。
[root@localhost ~] # /usr/local/apache2/bin/apachectl restart

(3).下面是效果

1
2
3
4
5
6
7
[root@localhost ~] # cd /log/www/
[root@localhost www] # ll
total 15072
-rw-r--r-- 1 root root   16028 Dec 26 15:16 access_20131225.log
-rw-r--r-- 1 root root 2406307 Dec 26 23:59 access_20131226.log
-rw-r--r-- 1 root root 8292792 Dec 27 23:59 access_20131227.log
-rw-r--r-- 1 root root 4682211 Dec 28 18:56 access_20131228.log



apache自带的日志分割工具rotatelogs,据专家说在进行日志切割时容易丢日志,这里也进行一下讲解:

Linux系统配置方法:
  将其改为
ErrorLog "| /usr/local/apache/bin/rotatelogs /usr/local/apache/logs/%Y_%m_%d_error_log 86400 480"  CustomLog "| /usr/local/apache/bin/rotatelogs /usr/local/apache/logs/%Y_%m_%d_access_log 86400 480" common
Windows系统下配置方法:
  #ErrorLog "|bin/rotatelogs.exe logs/error-%y%m%d.log 86400 480"
  #CustomLog "|bin/rotatelogs.exe logs/access-%y%m%d.log 86400 480" common

结合apache进行日志切割
  编辑Apache的主配置文件httpd.conf,更改内容如下:
  注释掉如下两行
  ErrorLog logs/error_log
  CustomLog logs/access_log common
  然后添加如下两行
  ErrorLog "|/usr/local/apache/bin/rotatelogs /usr/local/apache/logs/errorlog.%Y-%m-%d-%H_%M_%S 2M +480"
  CustomLog "|/usr/local/apache/bin/rotatelogs /usr/local/apache/logs/accesslog.%Y-%m-%d-%H_%M_%S 2M +480" common
  含义如下:
  errorlog.%Y-%m-%d-%H_%M_%S为生成日志格式,类似这样:errorlog.2010-04-15-11_32_30 ,以年月日时分秒为单位的,
  2M 为日志大小,即为日志达到多大后生成新的日志文件,支持的单位为K,M,G,本处为2M。
  +480 为时差,文件的时间为美国时间,中国的时差要比美国多8个小时也就是480分钟,所以要加上480分钟。


rotatelogs相关说明:

    rotatelogs logfile [ rotationtime [ offset ]] | [ filesizeM ]

rotationtime设定多少秒后进行日志切割,如这里的300秒,在设定的时间之后系统将自动切割日志;

offset指日志时间的偏移量,如果不设置该偏移量,则默认为0,且显示的时间与北京时间会不一样,与北京时间相差8个小时,故建议将其设置为+480,单位为分钟;

filesizeM指日志多大之后自动切割,可接受的单位为K,M,G,上面的ErrorLog设置为400M之后自动切割日志。

  其他的设置方法如下:
  每天生成一个错误日志文件
  ErrorLog "|bin/rotatelogs.exe -l logs/error-%Y-%m-%d.log 86400"
  其中86400为轮转的时间单位为秒
  rotatelogs - 滚动Apache日志的管道日志程序。
  CustomLog "|bin/rotatelogs /var/logs/logfile 5M" common
  此配置会在日志文件大小增长到5兆字节时滚动该日志。
  ErrorLog "|bin/rotatelogs /var/logs/errorlog.%Y-%m-%d-%H_%M_%S 5M"
  此配置会在错误日志大小增长到5兆字节时滚动该日志,日志文件名后缀会按照如下格式创建:errorlog.YYYY-mm-dd-HH_MM_SS 。
  基本语法
  rotatelogs [ -l ] logfile [ rotationtime [ offset ]] | [ filesizeM ]
  选项
  -l
  使用本地时间代替GMT时间作为时间基准。注意:在一个改变GMT偏移量(比如夏令时)的环境中使用-l会导致不可预料的结果。
  logfile
  它加上基准名就是日志文件名。如果logfile中包含"%",则它会被视为用于strftime()的格式字符串;否则它会被自动加上以秒为单位的".nnnnnnnnnn"后缀。这两种格式都表示新的日志开始使用的时间。
  rotationtime
  日志文件滚动的以秒为单位的间隔时间。
  offset
  相对于UTC的时差的分钟数。如果省略,则假定为"0"并使用UTC时间。比如,要指定UTC时差为"-5小时"的地区的当地时间,则此参数应为"-300"。
  filesizeM
  指定以filesizeM文件大小滚动,而不是按照时间或时差滚动。