日志切割

日志切割

当我们每访问一次网站时,会有若干条日志,即访问日志。前提是之前已经配置了日志工能。但当一个网站的访问量巨大时,记录的日志也是巨量的。这样即不利于日志的查看也会对服务硬盘资源造成浪费。
要解决这种情况就需要对日志做切割,即将日志按照需求进行归档

以天为单位对访问日志进行归档,配置如下:

  1. <VirtualHost *:80>
  2. DocumentRoot "/data/www/"
  3. ServerName www.test.com
  4. ErrorLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test-error_%Y%m%d.log 86400"
  5. CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test-access_%Y%m%d.log 86400" combined
  6. </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写在一行,中间不可换行,否则会报如下错误

  1. AH00526: Syntax error on line 33 of /usr/local/apache2/conf/extra/httpd-vhosts.conf:
  2. 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中:

  1. <IfModule log_config_module>
  2. #
  3. # The following directives define some format nicknames for use with
  4. # a CustomLog directive (see below).
  5. #
  6. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
  7. LogFormat "%h %l %u %t \"%r\" %>s %b" common
  8. <IfModule logio_module>
  9. # You need to enable mod_logio.c to use %I and %O
  10. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
  11. </IfModule>
  12. #
  13. # The location and format of the access logfile (Common Logfile Format).
  14. # If you do not define any access logfiles within a <VirtualHost>
  15. # container, they will be logged here. Contrariwise, if you *do*
  16. # define per-<VirtualHost> access logfiles, transactions will be
  17. # logged therein and *not* in this file.
  18. #
  19. CustomLog "logs/access_log" common
  20. #
  21. # If you prefer a logfile with access, agent, and referer information
  22. # (Combined Logfile Format) you can use the following directive.
  23. #
  24. #CustomLog "logs/access_log" combined
  25. </IfModule>




posted @ 2016-01-17 17:38  考鸡蛋  阅读(281)  评论(0编辑  收藏  举报