NodeJS/Forever存档日志

los*_*ion 18 node.js winston forever

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

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

我刚刚开始考虑使用Winston作为我的记录器,我没有遇到任何有用的东西.

有任何想法吗?

mak*_*mak 35

永远本身不支持日志轮换,日志轮换仍然是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.
}
Run Code Online (Sandbox Code Playgroud)

查看更多logrotate示例.

  • Winston现在可以进行日志轮换:https://github.com/flatiron/winston/pull/205 (2认同)
  • `sharedscripts`:"sharedscripts意味着postrotate脚本只会运行一次(在旧日志被压缩之后),而不是每个被轮换的日志运行一次." [man logrotate](http://linuxcommand.org/man_pages/logrotate8.html) (2认同)