本文介绍了应用程序写入“.log.1"文件而不是“.log"运行 logrotate 后的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

强制运行 logrotate 后,我的应用程序不断写入 my_app.log.1(应稍后存档的旧日志)而不是 my_app.log.

After force running logrotate, my app keeps writing to my_app.log.1 (the old log that should be archived later) instead of my_app.log.

这使 my_app.log 成为一个空文件,因此 logrotate 运行没有任何影响.而且 my_app.log.1 不断增长到千兆字节.

This make my_app.log an empty file, therefore logrotate runs without any effect. And my_app.log.1 keep growing to gigabytes.

我正在运行 Ubuntu 12.04.我的应用程序是一个使用 pm2 的 Node.js 应用程序.以下是我的 logrotate 配置:

I am running Ubuntu 12.04. My app is a Node.js app using pm2. Following is my logrotate configuration:

"/var/log/my_app/*.log" {
  daily
  size 50M
  rotate 10
  missingok
  compress
  delaycompress
  notifempty
}

我知道我把 notifempty 放在那里,但为什么首先写入 my_app.log.1 ?

I know I put notifempty there, but why is my_app.log.1 written to in the first place?

推荐答案

我终于找到了解决问题的方法.

I finally figured out how to solve the problem.

这是因为日志文件是由 pm2 写入的.logrotate 更名为 my_app.log.1 并创建了新的 my_app.log 文件,但 pm2 没有关心这一点并不断写信给 my_app.log.1.

This was because the log file was being written by pm2. logrotate changed its name to my_app.log.1 and created new my_app.log file, but pm2 did not care about this and kept writing to my_app.log.1.

我通过用 copytruncate 替换 notifempty 选项解决了这个问题,然后重新启动了 pm2.修复后,notifempty 可以添加回来,但我真的不需要它.

I solved the problem by replacing the notifempty option by copytruncate and then restarted pm2. After fixing, notifempty can be added back but I do not really need it.

请参阅 logrotate 参考 了解更多信息.希望这能帮助其他遇到类似问题的人.

See the logrotate reference for more information. Hope this will help other folks getting a similar problem.

这篇关于应用程序写入“.log.1"文件而不是“.log"运行 logrotate 后的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 12:47