[[email protected] abc]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
修改为以日期为命名的错误日志和访问日志
ErrorLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/tpp.com-error_%Y%m%d.log 86400" 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-requestCustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/tpp.com-access_%Y%m%d.log 86400" combined env=!image-request
如下图:
其中命令都是以绝对路径,rotatelogs是分割工具,%Y%m%d时间为分割单位,86400秒为一天。SetEnv为自定义的,下面访问日志可以调用env,即不记录以gif、jpg等结尾的文件。
接着我们从新检测和加载Apache
[[email protected] abc]# /usr/local/apache2/bin/apachectl -t Syntax OK [[email protected] abc]# /usr/local/apache2/bin/apachectl graceful
然后我们刷新下网页就会看到有日志生成,如下图:
[[email protected] abc]# cd /usr/local/apache2/logs/ [[email protected] logs]# ls access_log dummy-host.example.com-access_log dummy-host.example.com-error_log error_log httpd.pid [[email protected] logs]# ls access_log dummy-host.example.com-access_log dummy-host.example.com-error_log error_log httpd.pid tpp.com-access_20160429.log
注意:日积月累访问日志会越来越多,下面我们写个脚本进行删除一个月前的访问日志:
[[email protected] logs]# cat /usr/local/apache2/logs/logcron.sh
#! /bin/bash
# delete access logs
keepdays=31
log_files="/usr/local/apache2/logs/tpp.com-access"
rm -f ${log_files}_$(date +"%Y%m%d" --date="-${keepdays} day").log
加入到计划任务中,每天零点执行:
[[email protected] logs]# crontab -e
00 00 * * * /bin/bash/ /usr/local/apache2/logs/logcron.sh
时间: 2024-10-13 00:13:49