retgb 发表于 2015-8-24 10:21:04

apache日志切割

apache在提供服务后,在/usr/local/apache2/logs下会默认产生访问日志和错误日志access_log和error_log
尤其是访问日志,在用户访问多的情况下,会产生很多条记录。随着时间增长,可能会变成几个G或十几个G。
为了缓解这种情况,按天来切割日志,删除以前没用的日志,就成了很好的解决方法。

首先来看系统默认产生的访问日志格式:
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>
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>

其中有combined common和combinedio三种格式类型
较常用的有combined类型
%h表示来源IP %u表示用户名 %t表示时间 %r表示动作,如:GET 详细说明,请查看官方手册

配置步骤:
1.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

2.修改访问日志的类型为combined
    CustomLog "logs/dummy-host.example.com-access_log" combined

3.添加切割命令,切割后日志绝对路径和自定义时间戳日志名称 最后添加切割条件,即24小时切割一次。
CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/%Y%m%d-access_log 86400" combined

注意:它都是在正点自动切割,按天分隔,那它在2015-01-03 23:59:59下一秒就会自动生成新的日志文件。

页: [1]
查看完整版本: apache日志切割