zoukankan      html  css  js  c++  java
  • nohup.out 日志切分

    前言

    接到研发这样一个需求,希望将启动微服务日志 nohup.out 进行切分,因为时间长了,日志会堆积很多无法查阅。

    我问:微服务不是可以每天生成一个日志来以日期进行切分吗?

    研发回答:有些报错信息程序无法获取并写入到日志文件中,所以 nohup.out 日志还是需要的。

    基于以上原因,决定来处理下 nohup.out 的日志切分。

    logrotate切分nohup

    以前也用过 logrotate 为 nginx 做过切分,这里就不再详细描述了。

    参考:https://www.cnblogs.com/hukey/p/12779448.html

    直接写 logrotate 脚本:

    [root@localhost ~]# vim /etc/logrotate.d/app_service 
    
    /data/app-1/nohup.out
    /data/app-2/nohup.out
    /data/app-3/nohup.out
     {
      create 0644
      daily
      dateext
      rotate 3
      missingok
      notifempty
      copytruncate
      compress
    }
    
    
    解析:
    
    需要切分的多个日志文件:
    /data/app-1/nohup.out
    /data/app-2/nohup.out
    /data/app-3/nohup.out
    
    
      create 0644 - 以指定的权限创建全新的日志文件
      daily - 日志文件切割频率 daily: 每日,monthly: 每月,weekly: 每周,yearly: 每年
      dateext - 使用日期作为命名格式
      rotate 10 - 一次将存储10个归档日志,对于第11个归档,时间最久的归档将删除
      missingok - 在日志轮询期间,任何错误将被忽略,例如:文件无法找到 之类的错误
      notifempty - 如果文件为空,将不会对日志进行归档
      copytruncate - 把当前log拷贝后截断。可以理解为把内容拷贝走作为备份,然后清空当前文件。但是这有一个问题就是拷贝和截断之间会有时间差,存在丢数据的可能。 
      compress - 在日志归档之后,对日志进行gzip压缩
    

    测试

    下面信息没有出现报错就OK

    [root@localhost ~]# logrotate -d -f /etc/logrotate.d/app_service 
    reading config file /etc/logrotate.d/app_service
    Allocating hash table for state file, size 15360 B
    
    Handling 1 logs
    
    rotating pattern: /data/app-1/nohup.out
    /data/app-2/nohup.out
    /data/app-3/nohup.out
      forced from command line (3 rotations)
    empty log files are not rotated, old logs are removed
    considering log /data/app-1/nohup.out
      log needs rotating
    considering log /data/app-2/nohup.out
      log needs rotating
    considering log /data/app-3/nohup.out
      log needs rotating
    rotating log /data/app-1/nohup.out, log->rotateCount is 3
    dateext suffix '-20210324'
    glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
    glob finding old rotated logs failed
    copying /data/app-1/nohup.out to /data/app-1/nohup.out-20210324
    truncating /data/app-1/nohup.out
    compressing log with: /bin/gzip
    rotating log /data/app-2/nohup.out, log->rotateCount is 3
    dateext suffix '-20210324'
    glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
    glob finding old rotated logs failed
    copying /data/app-2/nohup.out to /data/app-2/nohup.out-20210324
    truncating /data/app-2/nohup.out
    compressing log with: /bin/gzip
    rotating log /data/app-3/nohup.out, log->rotateCount is 3
    dateext suffix '-20210324'
    glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
    glob finding old rotated logs failed
    copying /data/app-3/nohup.out to /data/app-3/nohup.out-20210324
    truncating /data/app-3/nohup.out
    compressing log with: /bin/gzip
    
    

    强制执行一次,主要是为了检测:

    [root@localhost ~]# logrotate -f /etc/logrotate.d/app_service 
    
    ### 查看备份 ###
    [root@localhost ~]# tree -a /data/ 
    /data/
    ├── app-1
    │   ├── nohup.out
    │   ├── nohup.out-20210324.gz
    │   └── test.sh
    ├── app-2
    │   ├── nohup.out
    │   ├── nohup.out-20210324.gz
    │   └── test.sh
    ├── app-3
    │   ├── nohup.out
    │   ├── nohup.out-20210324.gz
    │   └── test.sh
    └── test.sh
    
    3 directories, 10 files
    

    这里是通过手动来触发的,logrotate 是如何自动触发呢?

    [root@localhost ~]# cat /etc/cron.daily/logrotate
    #!/bin/sh
    
    /usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
    EXITVALUE=$?
    if [ $EXITVALUE != 0 ]; then
        /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
    fi
    exit 0
    

    通过上面定义,logrotate 是通过 crond 每日来触发的,因此只要保证 crond启动,logrotate 就会每日执行一次。

    【完】

  • 相关阅读:
    select、poll和epoll
    Linux 常用命令之文件和目录
    SmartPlant Review 帮助文档机翻做培训手册
    SmartPlant Foundation 基础教程 3.4 菜单栏
    SmartPlant Foundation 基础教程 3.3 标题栏
    SmartPlant Foundation 基础教程 3.2 界面布局
    SmartPlant Foundation 基础教程 3.1 DTC登陆界面
    SmartPlant Foundation 基础教程 1.4 SPF架构
    SmartPlant Foundation 基础教程 1.3 SPF其他功能
    SmartPlant Foundation 基础教程 1.2 SPF集成设计功能
  • 原文地址:https://www.cnblogs.com/hukey/p/14573959.html
Copyright © 2011-2022 走看看