2013-03-05 45 views
17

我正在使用forever来运行我的节点应用程序。当我永远开始时,我指定在哪里写日志。我还指定附加到日志。这里的问题是,我的日志将在几个月内失控。NodeJS/Forever存档日志

是否有任何方法在间隔内存档/滚动日志,即每日滚动/存档日志文件中的内容到另一个文件(即server-2013-3-5.log)。这样我可以根据需要删除/移出旧的日志文件。

我刚开始研究使用温斯顿为我的记录器,我还没有遇到任何有帮助的东西。

任何想法?

+0

你到底是如何解决它的? – JustGoscha 2016-01-23 13:20:06

回答

34

永远本身不支持日志旋转和日志旋转仍然是Winston的pending feature request

您可以使用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. 
} 

more logrotate examples

+2

Winston现在可以进行日志轮换:https://github.com/flatiron/winston/pull/205 – JCM 2013-06-10 19:37:33

+2

'sharedscripts':“共享脚本意味着postrotate脚本只能运行一次(在旧日志被压缩后),对于每个旋转的日志都不会一次。“ [man logrotate](http://linuxcommand.org/man_pages/logrotate8.html) – 2014-04-30 12:31:52

+0

@JCM尽管如何在Winston中永久使用这个新功能呢?我是否必须从命令行使用切换到永远的编程使用? – sheldonh 2015-04-10 11:45:55