NodeJS/Forever archive logs

lostintranslation picture lostintranslation · Mar 5, 2013 · Viewed 14.1k times · Source

I am using forever to run my node application. When I start forever I specify where to write the logs. I also specify to append to the log. Problem here is that my log is going to grow out of control over the course of months.

Is there any way to archive/roll logs on an interval, i.e. every day roll/archive what is in the log file to another file (i.e. server-2013-3-5.log). That way I can delete/move off old log files as needed.

I have just started looking into using Winston for my logger and I have not come across anything there that would help.

Any ideas?

Answer

mak picture mak · Mar 5, 2013

forever itself doesn't support log rotation and log rotation is still a pending feature request for Winston.

You can use logrotate which is included in most Linux distributions and is used for rotating system log files, as well as used by other software like Apache.

Add a file to /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.
}

See more logrotate examples.