日志切割
当我们每访问一次网站时,会有若干条日志,即访问日志。前提是之前已经配置了日志工能。但当一个网站的访问量巨大时,记录的日志也是巨量的。这样即不利于日志的查看也会对服务硬盘资源造成浪费。
要解决这种情况就需要对日志做切割,即将日志按照需求进行归档
以天为单位对访问日志进行归档,配置如下:
<VirtualHost *:80>
DocumentRoot "/data/www/"
ServerName www.test.com
ErrorLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test-error_%Y%m%d.log 86400"
CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test-access_%Y%m%d.log 86400" combined
</VirtualHost>
注意将ErrorLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test-error_%Y%m%d.log 86400"
写成一行,
将CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test-access_%Y%m%d.log 86400" combined
写在一行,中间不可换行,否则会报如下错误
AH00526: Syntax error on line 33 of /usr/local/apache2/conf/extra/httpd-vhosts.conf:
Invalid command '/usr/local/apache2/logs/test-error_%Y%m%d.log', perhaps misspelled or defined by a module not included in the server configuration
分析:
ErrorLog “|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test-error_%Y%m%d.log 86400”
CustomLog “|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test-access_%Y%m%d.log 86400” combined
ErrorLog 错误日志 CostomLog 访问日志
| 管道符,将产生的日志交给rotatelogs工具,Apache自带切割日志工具
/usr/local/apache2/bin/rotatelogs 调用rotatelogs工具
-l 校准时区为UTC,即北京时间
/usr/local/apache2/logs/test-error_%Y%m%d.log
/usr/local/apache2/logs/test-access_%Y%m%d.log
将rotatelogs切割后的日志保存在apache安装目录下的Logs目录,并以日期重命名。
86400 单位秒,为一天,即日志第天切割一次
combined 日志格式,定义于http.conf中:
<IfModule log_config_module>
#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
LogFormat "%h %l %u %t "%r" %>s %b" common
<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i" %I %O" combinedio
</IfModule>
#
# The location and format of the access logfile (Common Logfile Format).
# If you do not define any access logfiles within a <VirtualHost>
# container, they will be logged here. Contrariwise, if you *do*
# define per-<VirtualHost> access logfiles, transactions will be
# logged therein and *not* in this file.
#
CustomLog "logs/access_log" common
#
# If you prefer a logfile with access, agent, and referer information
# (Combined Logfile Format) you can use the following directive.
#
#CustomLog "logs/access_log" combined
</IfModule>