站长资源
中国站长网站

设置Apache服务器每天生成网站日志,方便查看

我们知道apache的日志文件是每天都会记录,一点点增加,这样时间一长查看起来非常的不方便,那么有没有办法让其每天都生成日志,让我们能够一目了然的查看指定日期的日志呢,答案肯定是有的

可以使用rotatelogs来设置服务器的网站访问日志按天记录,每天生成一个单独的文件, 这样非常方便查看

以我使用的CentOS为例, 首先要检测是否带有rotatelogs.

如果没有安装,则需要我们安装rotatelogs, 运行命令:

yum install rotatelogs

修改apache的配置文件,按天记录网站的访问日志

打开apache的配置文件:vim /etc/httpd/conf/httpd.conf, 编辑:

#ErrorLog logs/error_log #注释掉这行,加上下面这行
ErrorLog "|rotatelogs /home/lao8.org/log/error_log%Y_%m_%d.log 86400 480"  #设置好日志保存目录每天生成错误日志
#CustomLog logs/access_log common #注释加上下面这样
CustomLog "|rotatelogs  /home/lao8.org/log//access_log%Y_%m_%d.log 86400 480" combined #每天生成访问日志

保存并重启apache (运行命令 service httpd restart)

备注:

1. Rotatelogs的用法:

rotatelogs [ -l ] logfile [ rotationtime [ offset ]] | [ filesizeM ]

选项:

-l    使用本地时间代替GMT时间作为时间基准。注意:在一个改变GMT偏移量(比如夏令时)的环境中使用-l会导致不可预料的结果。

logfile    它加上基准名就是日志文件名。如果logfile中包含"%",则它会被视为用于strftime()的格式字符串;否则它会被自动加上以秒为单位的".nnnnnnnnnn"后缀。这两种格式都表示新的日志开始使用的时间。
    
rotationtime    日志文件滚动的以秒为单位的间隔时间。
    
offset    相对于UTC的时差的分钟数。如果省略,则假定为"0"并使用UTC时间。比如,要指定UTC时差为"-5小时"的地区的当地时间,则此参数应为"-300"。
   
filesizeM    指定以filesizeM文件大小滚动,而不是按照时间或时差滚动。

2. Rotatelogs 失败不生效的原因和解决办法:

rotatelogs 截断日志,进行配置。但是保存之后,服务器restart 始终无法重启成功。

日志文件:

(2)No such file or directory: AH00089: Couldn't start ErrorLog process ' rotatelogs /var/www/html/logs/error/%Y/%m/%d_error.log 86400 480'.  
AH00015: Unable to open logs  
(2)No such file or directory: AH00089: Couldn't start ErrorLog process ' rotatelogs /var/www/html/logs/error/%Y/%m/%d_error.log 86400 480'.  
AH00015: Unable to open logs  
  
  
Could not open log file '/home/google.com/logs/access/2014/access__20141019.log' (No such file or directory)  
AH00106: piped log program '/usr/sbin/rotatelogs /home/google.com/logs/access/%Y/access__%Y%m%d.log 86400 480' failed unexpectedly  
Could not open log file '/home/google.com/logs/access/2014/access__20141019.log' (No such file or directory)  
AH00106: piped log program '/usr/sbin/rotatelogs /home/google.com/logs/access/%Y/access__%Y%m%d.log 86400 480' failed unexpectedly  
Could not open log file '/home/google.com/logs/access/2014/access__20141019.log' (No such file or directory)  
AH00106: piped log program '/usr/sbin/rotatelogs /home/google.com/logs/access/%Y/access__%Y%m%d.log 86400 480' failed unexpectedly  
Could not open log file '/home/google.com/logs/access/2014/access__20141019.log' (No such file or directory)  
AH00106: piped log program '/usr/sbin/rotatelogs /home/google.com/logs/access/%Y/access__%Y%m%d.log 86400 480' failed unexpectedly  

解决办法:

1)rotatelogs 不能直接写 rotatelogs,必须写完整地址 /usr/sbin/rotatelogs

2)rotatelogs 无创建目录的权限,所以最好不要给 格式化的地址加文件夹。

正确的写法:

CustomLog "| /usr/sbin/rotatelogs /var/www/html/logs/access_%Y%m%d.log 86400 480 " combined

本文出处:来自互联网信息共享,请勿相信收费信息站长资源 » 设置Apache服务器每天生成网站日志,方便查看

评论 抢沙发

评论前必须登录!