• 如何在Centos 7上用Logrotate管理日志文件


    何为Logrotate?

    Logrotate是一个实用的日志管理工具,旨在简化对系统上生成大量的日志文件进行管理。 Logrotate允许自动旋转压缩,删除和邮寄日志文件,从而节省宝贵的磁盘空间。 Logrotate可以设置为每天、每周、每月或当日志文件达到一定的大小时处理日志文件。还可以完全控制日志的自动化管理方式,而不需要人工干预。Logrotate支持Linux系统上的所有日志文件,包括但不限于ApacheNginxTomcatELKzabbix等应用。

    1.安装logrotate

    系统默认已经安装,某些Linux发行版可能并未安装,那么请执行以下命令安装:

    Centos:

    $ yum install logrotate -y

    Ubuntu:

    $ apt-get install logrotate -y

    通过yum默认安装的文件存储位置说明

    /etc/cron.daily            # logrotate脚本文件存放位置
    /etc/logrotate.conf        # 主配置文件
    /usr/sbin/logrotate        # 二进制程序文件
    /etc/logrotate.d/          # 特定服务日志存储目录(自己设定的配置文件)
    /var/lib/logrotate.status  # 状态文件

    2.查看logrotate主文件默认配置情况

    $ cat /etc/logrotate.conf
    
    # see "man logrotate" for details
    # rotate log files weekly
    weekly		            # 每周转存一次
    
    # keep 4 weeks worth of backlogs
    rotate 4                    # 保留四个日志备份文件
    
    # create new (empty) log files after rotating old ones
    create	                    # rotate后,创建一个新的空文件
    
    # use date as a suffix of the rotated file
    dateext                     # 轮转的文件名字带有日期信息
    
    # uncomment this if you want your log files compressed
    #compress
    
    # RPM packages drop log rotation information into this directory
    include /etc/logrotate.d    # 此目录下的配置文件优先生效
    
    # no packages own wtmp and btmp -- we'll rotate them here
    /var/log/wtmp {             # 指定/var/log/wtmp日志文件;
        monthly                 # 每月轮转一次,优先于全局设定的每周轮转一次;
        minsize 1M              # 日志文件大于1M才会去轮转;
        create 0664 root utmp   # 新日志文件的权限,属主,属组;
        rotate 1                # 保留一个日志备份,优先于全局设置的四个;
    }
    /var/log/btmp {             # 指定/var/log/btmp日志文件;
        missingok               # 如果日志丢失,不报错;
        monthly
        create 0600 root utmp
        rotate 1
    }
    
    # system-specific logs may be also be configured here.
    # 系统特定的日志也可以在主文件中配置。

    3.logrotate日志切割

    在定义日志文件时可以使用通配符,但不建议使用,因为它会包括已切换过的日志,如必须使用,请在*号后加上已知的文件扩展名, 例如:*.log

    Nginx日志:

    /etc/logrotate.d/目录下创建Nginx服务日志配置文件

    $ vim /etc/logrotate.d/nginx
    
    /usr/local/nginx/logs/*.log {
    create 644 www root
    daily
    dateext
    rotate 3
    minsize 10K
    copytruncate
    nocompress
    missingok
    notifempty
    noolddir
    postrotate
    /bin/kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
    endscript
    }

    php日志:

    /usr/local/php/var/log/*.log {
    missingok
    notifempty
    sharedscripts
    delaycompress
    create 0664 www www
    postrotate
    /bin/kill -SIGUSR1 `cat /usr/local/php/var/run/php-fpm.pid 2>/dev/null` 2>/dev/null || true
    endscript
    }

    注:你也可以手动生成一个20M内容的日志文件进行测试,例如:

    $ head -c 20M < /dev/urandom > /var/log/renwole-log

    配置完成后,可以通过如下命令来手动执行查看效果:

    $ logrotate -f /etc/logrotate.d/nginx

    另外还可以使用如下命令查看logrotate运行状态:

    $ cat /var/lib/logrotate/logrotate.status

    由于Logrotate是基于cron定时运行的,所以logrotate脚本默认在 /etc/cron.daily/ 目录下,文件名是logrotate。你可以设置 /etc/anacrontab 来控制Logrotate何时运行。

    4.查看anacrontab默认配置

    $ cat /etc/anacrontab
    
    # /etc/anacrontab: configuration file for anacron
    
    # See anacron(8) and anacrontab(5) for details.
    
    SHELL=/bin/sh
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    # the maximal random delay added to the base delay of the jobs
    RANDOM_DELAY=45
    # the jobs will be started during the following hours only
    START_HOURS_RANGE=3-22
    
    #period in days   delay in minutes   job-identifier   command
    1       5       cron.daily              nice run-parts /etc/cron.daily
    7       25      cron.weekly             nice run-parts /etc/cron.weekly
    @monthly 45     cron.monthly            nice run-parts /etc/cron.monthly

    5.关于logrotate常用配置参数注解

    daily,weekly,monthly  # 转储周期分别是每天/每周/每月;
    minsize 15M           # 日志体积大于此值时轮换(例如:100K,4M);
    dateext	              # 轮询的文件名字带有日期信息;
    missingok             # 如果日志文件丢失,不要显示错误;
    rotate 5              # 轮转存储中包含多少备份日志文件,0为无备份,以数字为准;
    compress              # 通过gzip压缩转储以后的日志,以*.gz结尾;
    nocompress            # 不需要压缩时,用这个参数;
    delaycompress         # 延迟压缩,和compress一起使用时压缩所有日志,除当前和下一个最近的;
    nodelaycompress       # 覆盖delaycompress选项,转储同时压缩;
    copytruncate          # 用于还在打开中的日志文件,把当前日志备份并截断;
    nocopytruncate        # 备份日志文件但是不截断;
    create 644 www root   # 转储文件,使用指定的文件模式创建新的日志文件;
    nocreate              # 不建立新的日志文件;
    errors renwole@my.org # 专储时的错误信息发送到指定的Email地址;
    ifempty               # 即使是空文件也转储,这个是logrotate的缺省选项;
    notifempty            # 如果日志文件为空,则不转储;
    mail renwole@my.org   # 把转储的日志文件发送到指定的E-mail地;
    nomail                # 转储时不发送日志文件;
    olddir /tmp           # 转储后的日志文件放入指定目录,必须和当前日志文件在同一个文件系统;
    noolddir              # 转储后的日志文件和当前日志文件放在同一个目录下;
    prerotate/endscript   # 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行;
    postrotate/endscript  # 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行;
    tabooext              # 不转储指定扩展名的文件,缺省扩展名:cfsaved,.disabled,.dpkg-dist等;
    sharedscripts         # 共享脚本,让postrotate/endscript包含脚本只执行一次即可;
    dateformat            # 配合dateext使用可以为切割后的日志加上YYYYMMDD格式的日期;

    以上参数都可以在全局主配置文件中定义,或指定为某个日志文件进行配置,注意:使用时参数之间不要冲突。

    python uwsgi 日志分割

    [root@test-server28 logrotate.d]# cat uwsgi
    /data/logs/uwsgi/*/access.log {
    # 每天分割
    daily
    # 保存30天
    rotate 30
    # 日期后缀
    dateext
    # 日志丢失不报错
    missingok
    # 空文件不转储
    notifempty
    # 压缩
    #compress
    # 共享脚本
    sharedscripts
    postrotate
    touch /data/logs/uwsgi/touchforlogrotate
    endscript
    }

    切割日志时出现报错 

    error: skipping "/home/deploy/tomcat/apache-tomcat-7.0.75-df/logs/catalina.out" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.
    

      

    xx 文件所属用户
    
    添加“su deploy deploy ”到/etc/logrotate.d/tomcat文件中即可 
    如下;
    
    
    /home/deploy/tomcat*********/logs/*.out {
            su deploy deploy
            copytruncate
            daily
            rotate 30
            dateext
            missingok
            notifempty
    }
    

      

    [root@**** logrotate.d]#  logrotate -f /etc/logrotate.d/tomcat
    
    
    [root@**** logs]# ls
     catalina.out            catalina.out-20181210  
    

      

    对分割日志进行定时的删除 
    
    #!/bin/bash
    
    
    /bin/find /mnt/log/tomcat/ -name "*.out*" -mtime +15 -type f|xargs rm -rf
    
    /bin/find /home/deploy/*******/logs/  -name "*.out*" -mtime +1 -type f -print0 |xargs -0 mv -t /mnt/log/tomcat/ 
    

      

  • 相关阅读:
    Hexo+Github 搭建一个自己的博客
    vue中sessionStorage存储的用法和问题
    vue 页面刷新
    vue渲染完页面后div滚动条定位在底部
    vue 定义全局函数
    vue filter过滤器用法
    vue中bus.$on事件被多次绑定
    vue中引入jQuery的方法
    vue2.0传值方式:父传子、子传父、非父子组件、路由跳转传参
    vue打包后显示为空白页的解决办法
  • 原文地址:https://www.cnblogs.com/weifeng1463/p/8427851.html
Copyright © 2020-2023  润新知