Apache访问日志切割

[root@daixuan ~]# 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日志格式定义:

[root@daixuan ~]# vim /usr/local/apache2/conf/httpd.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>

%h           来源IP 192.168.101.175

%u           用户  user1

%t           时间  

\"%r\"        动作,get

"%{Referer}i\"   链接地址

"%{User-Agent}i  浏览器的Agent

[root@daixuan logs]# tail -10 test.com-access_log

192.168.101.175 - - [01/Dec/2015:14:36:52 +0800] "GET /misc.php?mod=patch&action                                                 =pluginnotice&inajax=1&ajaxtarget=plugin_notice HTTP/1.1" 200 63


每天都会有大量的日志,该怎么处理呢?

[root@daixuan logs]# 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==一天

[root@daixuan logs]# ls /usr/local/apache2/logs       此时日志按照天分割

test.com-access_20151201_log  test.com-error_log


Apache如何做到不记录指定文件类型日志?

[root@daixuan logs]# !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......才会记录到日志中。

[root@daixuan logs]# apachectl -t

Syntax OK

[root@daixuan logs]# apachectl restart

重新访问浏览器,打开几个测试页面,查看日志,发现gif,jpg,png....结尾的文件都没有记录到日志

你可能感兴趣的:(Apache访问日志切割)