zoukankan      html  css  js  c++  java
  • nginx日志切割

    http://www.ttlsa.com/nginx/nginx-log-cutting/

    nginx日志默认情况下统统写入到一个文件中,文件会变的越来越大,非常不方便查看分析。以日期来作为日志的切割是比较好的,通常我们是以每日来做统计的。下面来说说nginx日志切割。
    关于nginx相关日志配置参见:《nginx日志配置》一文。logrotate用法参见《logrotate日志管理工具》。

    1. 定义日志轮滚策略

    # vim nginx-log-rotate

    /data/weblogs/*.log {
        nocompress
        daily
        copytruncate
        create
        notifempty
        rotate 7
        olddir /data/weblogs/old_log
        missingok
        dateext
        postrotate
            /bin/kill -HUP `cat /var/run/nginx.pid 2> /dev/null` 2> /dev/null || true
        endscript
    }
    /data/weblogs/*.log使用通配符时,/data/weblogs/目录下的所有匹配到的日志文件都将切割。如果要切割特定日志文件,就指定到该文件。

    2. 设置计划任务

    # vim /etc/crontab

    59 23 * * * root ( /usr/sbin/logrotate -f /PATH/TO/nginx-log-rotate)

    这样每天23点59分钟执行日志切割。

    如需转载请注明出处: http://www.ttlsa.com/html/3166.html

    在linux环境下,我希望能够每天到0点切换nginx日志,前一天的日志使用日期后缀,并且压缩。

     
    步骤:
    1.
    需要制作一个nginxlogrotate文件,放在目录/etc/logrotate.d/下,
    文件内容如下:
    /somedir1/*.log {
        daily
        missingok
        rotate 65535
        compress
        dateext
        notifempty
        sharedscripts
        postrotate
            [ ! -f /somedir2/nginx.pid ] || kill -USR1 `cat /somedir2/nginx.pid`
        endscript
    }
     
    其中somedir1需要替换成存放nginx日志的目录,somedir2需要替换成系统存放nginx.pid文件的目录。
     
    2.
    定时启动logrotate
    设置定时任务,crontab -e
    0 0 * * * nohup /usr/sbin/logrotate /etc/logrotate.conf > /dev/null 2>&1 &
     
    3.隔天查看nginx日志目录,发现生成了如下的文件
    access.log-20121008.gz
    error.log-20121008.gz

    参见:
     
     
     
     
     
    http://drumcoder.co.uk/blog/2012/feb/03/nginx-and-logrotate/

    nginx and Logrotate

    February 3, 2012

    I wanted to start cycling and compressing my previous nginx log files, as they were getting a little large. NGINX doesn't have built in support for log rotation, there is a system level linux command called logrotate that handles this on behalf of many applications. I'm doing this on a Debian squeeze.

    /etc/logrotate.d

    In /etc/logrotate.d you'll find a file for each application that is using logrotate. There should be one in here called nginx that contains the following:

    /var/log/nginx/*.log {
        daily
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        postrotate
                [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
        endscript
    }
    

    This is the default rotate process for nginx files. I store my logs in /home/drumcoder/logs, so we will specify another block in this same file, using many of the same options.

    /home/drumcoder/log/*.access.log {
        daily
        missingok
        rotate 52
        mail drumcoder@here.com
        compress
        delaycompress
        notifempty
        sharedscripts
        postrotate
                [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
        endscript
    }
    

    The following options are used:

    • daily - rotates the log files daily
    • missingok - it's fine if there is no log to rotate
    • rotate 52 - rotate logs 52 times, and then delete the oldest
    • mail drumcoder@here.com - before you delete an old log, send it to this mail address
    • compress - use gzip on old logs
    • delaycompress - leave one rotated log that isn't compressed so the process can still write to it if needed
    • notifempty - don't rotate empty file
    • sharedscripts - when telling nginx that logs have been rotated, only do it once rather than once for each file group

    The bottom postrotate block sends a signal to nginx to tell it that logs have been rotated and it should pick up the file handles for the new ones.

    References

  • 相关阅读:
    Windows Store App 主题动画
    Windows Store App 过渡动画
    Windows Store App 控件动画
    Windows Store App 近期访问列表
    Windows Store App 文件选取器
    Windows Store App 访问应用内部文件
    Windows Store App 用户库文件分组
    Windows Store App 获取文件及文件夹列表
    Windows Store App 用户库文件夹操作
    Windows Store App 用户库文件操作
  • 原文地址:https://www.cnblogs.com/DjangoBlog/p/3949403.html
Copyright © 2011-2022 走看看