seanmonstar / intel

I need more intel!
http://seanmonstar.github.io/intel/
Mozilla Public License 2.0
202 stars 26 forks source link

Provide stream reopen capability for logrotation #39

Closed mfn closed 4 years ago

mfn commented 9 years ago

I couldn't find a way with intel to reopen streams when logrotation happens.

A common scenario is that a process writes to a file, linux logrotation utility moves the file and signals the application to recreate the primary one:

AFAICS intel would needs at least two things to provide this:

Here's a way how did it for my application for now. I created a ReopenableFileHandler:

var FileHandler = require('intel').handlers.File;

/**
 * Extends intels filehandler and provides #_reopen() to be manually called
 * whenever an external event moves the logfiles
 */
export default class ReopenableFileHandler extends FileHandler {
  _reopen() {
    if (this._stream) {
      this._stream.end();
      delete this._stream;
    }
    this._stream = this._open();
  }
}

In my application I provide a helper to look into intel which handlers expose _reopen call it when I receive an external signal (common is SIGUSR2 on linux):

function reopenLogfiles() {
  for (let i = 0; i < intel._handlers.length; i++) {
    if (typeof intel._handlers[i]._reopen === 'function') {
      intel._handlers[i]._reopen();
    }
  }
}

Maybe these snippets can serve as an inspiration.