繁体   English   中英

实施日志轮换的最佳方法是什么? (节点应用程序)

[英]What is the best way to implement log rotation? (Node application)

随着文件大小的不断增加,我需要旋转由节点进程创建的日志文件。 应用程序在AWS ec2实例上的ubuntu中运行。

我运行的永久节点进程很少,它将日志写入以下文件。

forever.log
stderr.log
stdout.log

我的CloudWatch代理还查看这些文件,并将日志推送到cloudwatch,并且节点应用程序也不断将日志写入这些文件中。

我需要找到轮换这些日志文件并清理较旧文件的最佳方法,而又不影响上述任何过程。

不确定以下方法会导致麻烦,因为它将移动文件并创建新文件

#!/bin/bash 
touch /script_logs/test.log
MaxFileSize=2048
while true
do
     sh test.sh >> /script_logs/test.log
#Get size in bytes** 
    file_size=`du -b /script_logs/test.log | tr -s '\t' ' ' | cut -d' ' -f1`
    if [ $file_size -gt $MaxFileSize ];then   
        timestamp=`date +%s`
        mv /script_logs/test.log /script_logs/test.log.$timestamp
        touch /script_logs/test.log
    fi

done

需要实现日志轮换机制,而不影响读取日志文件的Cloudwatch代理或写入日志的应用程序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM