19

永远使用来运行我的节点应用程序。当我永远开始时,我会指定在哪里写入日志。我还指定附加到日志。这里的问题是我的日志将在几个月内失去控制。

有没有办法按时间间隔归档/滚动日志,即每天将日志文件中的内容滚动/归档到另一个文件(即 server-2013-3-5.log)。这样我就可以根据需要删除/移走旧的日志文件。

我刚刚开始考虑将 Winston 用于我的记录器,但我还没有遇到任何有用的东西。

有任何想法吗?

4

1 回答 1

35

forever 本身不支持日志轮换,并且日志轮换仍然是 Winston 的待处理功能请求

您可以使用logrotate它包含在大多数 Linux 发行版中,用于轮换系统日志文件,以及被 Apache 等其他软件使用。

将文件添加到/etc/logrotate.d/

/path/to/server.log {
  daily         # how often to rotate
  rotate 10     # max num of log files to keep
  missingok     # don't panic if the log file doesn't exist
  notifempty    # ignore empty files
  compress      # compress rotated log file with gzip
  sharedscripts # postrotate script (if any) will be run only once at the end, not once for each rotated log
  copytruncate  # needed for forever to work properly
  dateext       # adds date to filename 
  dateformat %Y-%m-%d.
}

查看更多logrotate示例

于 2013-03-05T19:18:52.577 回答