apache 的日志轮询

原创
2016-06-07 15:11:10 673浏览

【疑问】 将Httpd.conf 配置文件中的CustomLog 参数改为下面的,就可以将 日志 按每天生成一个 日志 文件. CustomLog "|bin/rotatelogs.exe logs/access_%Y%m%d.log 86400 480" common rotatelogs.exe 就是apache提供用来做多 日志 文件的处理程序,从上面配

【疑问】

将Httpd.conf 配置文件中的CustomLog 参数改为下面的,就可以将日志按每天生成一个日志文件.

CustomLog "|bin/rotatelogs.exe logs/access_%Y%m%d.log 86400 480" common

rotatelogs.exe 就是apache提供用来做多日志文件的处理程序,从上面配置来看,apache实际上是通过“管道”(由命令前的竖线|判断)将要输出的日志信息传给rotatelogs.exe程序,由该程序负责日志的分割。

86400 是指每 86400 ( 一天的秒数 ) 86400为轮转的时间,单位为秒。86400s=24H=1天
, 480 指 GMT + 8 ( 以分钟计 , GMT -1 下 -60 ) .这样 Apache 就会自动在该换日时换一个档案名称.480为时差,文件的时间为美国时间,中国的时差要比美国多8个小时也就是480分钟,所以要加上480分钟。
rotatelogs说明

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

选项
logfile
它加上基准名就是日志文件名。如果logfile中包含’%',则它会被视为用于的strftime(3)的格式字串;否则,它会被自动加上以秒为单位的.nnnnnnnnnn后缀。这两种格式都表示新的日志开始使用的时间。
rotationtime
日志文件回卷的以秒为单位的间隔时间
offset
相对于UTC的时差的分钟数。如果省略,则假定为0,并使用UTC时间。比如,要指定UTC时差为-5小时的地区的当地时间,则此参数应为-300。
filesizeM
指定回卷时以兆字节为单位的后缀字母M的文件大小,而不是指定回卷时间或时差。

【实例讲解】

CustomLog "|/application/apache/bin/rotatelogs /app/logs/access_%Y-%m-%d-%H_%M_%S 2M +480" combined

详解:

access_%Y-%m-%d-%H_%M_%S为生成日志的格式,类似于这样:access_2010-04-15-11_32_30,以年月日时分秒为单位。

2M 为日志的大小,即为日志达到多大后生成新的日志文件,支持的单位为K,M,G,本处为2M。


【疑问】

CustomLog "|/application/apache/bin/rotatelogs /app/logs/access_%Y-%m-%d-%H_%M_%S 2M +480" combined

+480 前面有加号和没有加号有什么区别?如果是减号是什么意思?

测试如下:

[root@wanqiu extra]# date
2012年 01月 29日 星期日 16:29:45 CST

格式1:【不加480】

CustomLog "|/application/apache/bin/rotatelogs /app/logs/access_%Y-%m-%d-%H_%M_%S 2M" combined

重启apache后查看日志,显示如下:

[root@wanqiu extra]# ll /app/logs/
总计 8
-rw-r--r-- 1 root root 172 01-29 16:29 access_2012-01-29-08_29_45

格式2:【加480,前面没有+号】

CustomLog "|/application/apache/bin/rotatelogs /app/logs/access_%Y-%m-%d-%H_%M_%S 2M 480" combined

重启apache后查看日志,显示如下:

[root@wanqiu extra]# ll /app/logs/
总计 16
-rw-r--r-- 1 root root 172 01-29 16:29 access_2012-01-29-08_29_45
-rw-r--r-- 1 root root 172 01-29 16:32 access_2012-01-29-16_32_52

格式3:【加480,前面有+号】

CustomLog "|/application/apache/bin/rotatelogs /app/logs/access_%Y-%m-%d-%H_%M_%S 2M +480" combined

重启apache后查看日志,显示如下:

[root@wanqiu extra]# ll /app/logs/
总计 24
-rw-r--r-- 1 root root 172 01-29 16:29 access_2012-01-29-08_29_45
-rw-r--r-- 1 root root 172 01-29 16:32 access_2012-01-29-16_32_52
-rw-r--r-- 1 root root 172 01-29 16:37 access_2012-01-29-16_37_39

格式4:【加480,前面有-号】

CustomLog "|/application/apache/bin/rotatelogs /app/logs/access_%Y-%m-%d-%H_%M_%S 2M -480" combined

重启apache后查看日志,显示如下:

[root@wanqiu extra]# ll /app/logs/
总计 32
-rw-r--r-- 1 root root 172 01-29 16:41 access_2012-01-29-00_41_48
-rw-r--r-- 1 root root 172 01-29 16:29 access_2012-01-29-08_29_45
-rw-r--r-- 1 root root 172 01-29 16:32 access_2012-01-29-16_32_52
-rw-r--r-- 1 root root 172 01-29 16:37 access_2012-01-29-16_37_39

初秋

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。