日志旋转工具,只保留指定数量的日志,并放弃一切

我正在寻找一个类似cronolog的工具,只保留最后n行或最后x分钟的日志,并丢弃所有其他的东西

有这么一个野兽吗?

更新:

我知道logrotate,它重命名和压缩旧的日志文件,这不是我想要的。

我想丢弃旧的日志行,只保留最近的行。

就像ie每隔一段时间这样做:tail -10000 logfile> logfile.new mv logfile.new logfile除了使用这种技术之外,您肯定会丢失日志行,您必须重新启动或者以其他方式发信号通知日志logging应用程序重新打开日志文件。

Logrotate可以只保留一个日志文件的副本…如果你使用RTFM,你可以在configuration设置中find以下几点:

rotate count Log files are rotated count times before being removed or mailed to the address specified in a mail directive. If count is 0, old versions are removed rather than rotated. 

您可以再次使用logrotate(8)手册页中的大小进行 旋转 ,以使文件大小保持较小。 虽然不是由行数,而是由k,M,G大小。

 size size Log files are rotated when they grow bigger than size bytes. If size is followed by M, the size if assumed to be in megabytes. If the G suffix is used, the size is in gigabytes. If the k is used, the size is in kilobytes. So size 100, size 100k, and size 100M are all valid. 

你可以使用logrotate并放置

 tail -10000 logfile.0 > logfile.0.new mv logfile.0.new > logfile.0 

作为postrotate命令的一部分。 logrotate允许你指定postrotate命令。

无论如何,你将不得不重新启动或发信号。 应用程序不知何故必须了解seek()的新偏移量,或者在修剪日志文件时重新打开文件句柄。

不完全是你在说什么,但你可以检查logrotate(8) 。 从手册页:

logrotate旨在简化对生成大量日志文件的系统的pipe理。 它允许自动旋转,压缩,删除和邮寄日志文件。 每个日志文件可能会被处理…当它变得太大。

它默认安装在RHEL和衍生产品上。 我不知道任何其他像Ubuntu /派生或Windows。

最后,我已经解决了这个问题(不是最优雅的,但它的工作原理):

在Apache(或谁login):

CustomLog "|/usr/local/cronolog/sbin/cronolog /var/tmp/mylog.%Y%m%d.log" logformat

在cron.daily中:

find /var/tmp/ -name mylog* -mtime +$days | xargs --no-run-if-empty rm

这将删除旧的日志

最后在分析脚本中:

lastdate = $(date -d“$ INTERVAL sec ag​​o”+%Y-%m-%dT%H:%M:%S)

grep -h $ SEARCH / var / tmp / mylog * | awk -v lastdate =“$ lastdate”$ 1> lastdate {print}'> / tmp / cutlog

然后使用/ tmp / cutlog

上面的例子假定ISO时间戳在第一个字段中,如:2009-07-20T13:52:32

不是最优雅的方式,但它做我想要的。 也许有一天我会写cronolog的function,会做同样的事情:)