1、为什么要切割日志?
web服务器出现大量的访问日志和错误日志,所以我们要对日志进行切割,方便管理人员的查询。一些并不重要的记录,可以规定日志中不显示这些。
打开虚拟主机配置文件里的日志记录,日志的路径在/usr/local/apache2/logs下
ErrorLog “1ogs/test.com-error-1og” #错误日志
customLog “1ogs/test.com-access_1og" common #common 是日志类型
2、在apache的主配置文件里日志的相关格式规定
LogFormat “%h %1 %u %t \“%r\” %>s %b “%{Referer}i” \“%{user-Agent}i""
combined
LogFormat "%h %1 %u %t \“%r\” %s %b” common
-%h 是来源ip
-%u 访问的user
-%t 时间
-%r 动作
有些时候一些用户使用代理服务器,这里的%h会显示代理ip,在%h后面加上%(X-FORWARDED-FOR}i即显示真实ip。
3、修改配置文件,定义日志切割格式
#vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 修改配置文件
<VirtualHost *:80>
DocumentRoot “/data/www”
ServerName www.test.com
CustomLog “|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test.com-access_%Y%m%d_log 86400” combined #每天切割一次
#/usr/local/apache2/bin/apachectl restart //重启服务
#date //查看时间
#date 时间 //月日时分,比本机现在时间过去一天
#/usr/local/apache2/bin/apachectl restart //重启服务
#curl 192.168.234.138 //产生访问日志
#cd /usr/local/apache2/logs/
#ls //查看产生的日志
4、不记录指定文件类型的日志
ErrorLog “1ogs/test.com-error-1og"
SetEnvIf Request_URI “..gifKaTeX parse error: Expected 'EOF', got '#' at position 18: …image-request #̲对gif文件做一个标记imag…" image-request #setenvif 要和customlog 写在一起
SetEnvIf Request_URI "..pngKaTeX parse error: Can't use function '\.' in math mode at position 41: …Request_URI ".*\̲.̲bmp” image-request
SetEnvIf Request_URI “..swfKaTeX parse error: Can't use function '\.' in math mode at position 41: …Request_URI ".*\̲.̲js" image-request
SetEnvIf Request_URI "..dss$” image-request customLog "|/usr/1ocal/apache2/bin/rotatelogs -1
/usr/local/apache2/logs/test.com-access_%Y%n%d_1og 86400” combined env=limage-request #不记录做过标记的文件
注意:SetEnvlf要和customlog写在一起,中间不要有分割