zj2092 发表于 2018-11-20 12:49:51

Apache访问日志切割

  # vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
  修改日志文件为:

  ErrorLog "logs/test.com-error_log"       logs目录位置是相对于/usr/local/apache2
  CustomLog "logs/test.com-access_log" combined
  

  Apache的common日志格式定义:
  # vim /usr/local/apache2/conf/httpd.conf
  
  # 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
  
  %h         来源IP 192.168.101.175
  %u         用户user1
  %t         时间
  \"%r\"      动作,get
  "%{Referer}i\"   链接地址
  "%{User-Agent}i浏览器的Agent
  # tail -10 test.com-access_log
  192.168.101.175 - - "GET /misc.php?mod=patch&action                                                 =pluginnotice&inajax=1&ajaxtarget=plugin_notice HTTP/1.1" 200 63
  

  每天都会有大量的日志,该怎么处理呢?
  # vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
  ErrorLog "logs/test.com-error_log"
  CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test.com-access_%Y%m%d_log 86400" combined
  86400s==一天
  # ls /usr/local/apache2/logs       此时日志按照天分割
  test.com-access_20151201_logtest.com-error_log
  

  Apache如何做到不记录指定文件类型日志?
  # !vim
  vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
      SetEnvIf Request_URI ".*\.gif$" image-request
      SetEnvIf Request_URI ".*\.jpg$" image-request
      SetEnvIf Request_URI ".*\.png$" image-request
      SetEnvIf Request_URI ".*\.bmp$" image-request
      SetEnvIf Request_URI ".*\.swf$" image-request
      SetEnvIf Request_URI ".*\.js$" image-request
      SetEnvIf Request_URI ".*\.css$" image-request
  ErrorLog "logs/test.com-error_log"
  CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test.com-access_%Y%m%d_log 86400" combined env=!image-reqest
  文件结尾不是:gif,jpg,png......才会记录到日志中。
  # apachectl -t
  Syntax OK
  # apachectl restart
  重新访问浏览器,打开几个测试页面,查看日志,发现gif,jpg,png....结尾的文件都没有记录到日志



页: [1]
查看完整版本: Apache访问日志切割