LAMP 2.0Apache日志切割

每次访问网站就会产生若干条日志,当然前提是已经配置了日志。

配置日志的文件在

vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 

把注释掉的这两行打开

ErrorLog "logs/dummy-host.example.com-error_log"
CustomLog "logs/dummy-host.example.com-access_log" common

common是日志的格式,把日志的格式改成combined

   ErrorLog "logs/dummy-host.example.com-error_log"
   CustomLog "logs/dummy-host.example.com-access_log" combined

日志的位置 logs是针对/usr/locol/apache2来说的。生成的文件是后面的两个名字。可以更名。

   ErrorLog "logs/denny.com-error_log"
   CustomLog "logs/denny.com-access_log" combined

检查文件是否正确

apachectl -t

重新加载或者重启

/usr/local/apache2/bin/apachectl restart
/usr/local/apache2/bin/apachectl graceful

去论坛刷新几次,看看是否生成日志文件。

cd /usr/local/apache2/logs/
ls

生成了访问日志denny.com-access_log和错误日志denny.com-error_log

查看访问日志。

cat denny.com-access_log

复制其中一条信息

192.168.1.114 - - [08/Dec/2015:23:21:27 +0800] "GET /forum.php?mod=ajax&action=forumchecknew&fid=2&time=1449587726&inajax=yes HTTP/1.1" 200 64 "http://www.houchangfu.com/forum.php?mod=forumdisplay&fid=2" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)"

格式是

LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined

h来源IP 。l和u不存在用-号表示。u用户,t时间,r动作,Referer来源地址。User-Agent来源浏览器。

为了防止日志无限增长,要把每天的日志切割,以日期重命名,然后超过30天的删掉。

打开配置文件

vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 

修改

CustomLog "logs/denny.com-access_log" combined

CustomLog "|/usr/local/apache2/bin/rotatelogs -l  /usr/local/apache2/logs/denny.com-access_%Y%m%d_log 86400" combined

用apache2的命令,-l切割,日志存放路径写据对路径,时间格式 86400秒等于1天。

查看文件格式,重启。

apachectl -t
/usr/local/apache2/bin/apachectl restart

刷新论坛然后ls查看

 ls /usr/local/apache2/logs/
denny.com-access_20151208_log
原文地址:https://www.cnblogs.com/wangshaojun/p/5031102.html