0

I have redirected console.log() to a file(app_stdout.log) Next I run my program. app_stdout.log file is getting created. But If delete the log file manually when program is running then app_stdout.log file is not getting created again. Why? What Im missing here? is there any way i can catch error from the WriteStream?

var stdout = fs.createWriteStream("app_stdout.log", {flags: 'a'});

     process.__defineGetter__('stdout', function () {
          return stdout;
     });
Somnath
  • 3,247
  • 4
  • 28
  • 42

1 Answers1

1

At least on Unix, deleting a file removes its directory entry, but any processes that have the file open will still be able to write to it (in other words, it's not an error condition).

A common method of letting a process know that it should reopen (/recreate) a logfile is by sending it a signal (USR1, for instance) to tell it that it should close the previous file handle (to the deleted file) and reopen the logfile (a new instance of it).

A simple PoC using logstream:

var LogStream = require('logstream').LogStream;
var stdout    = new LogStream('/tmp/test.log');

process.__defineGetter__('stdout', function () {
  return stdout;
});

setInterval(function() {
  console.log(new Date().toString());
}, 1000);

process.on('SIGUSR1', function() {
  stdout.reopen();
});

Alternatively, you could periodically check if the logfile is still accessible from the file system and reopen if it isn't.

robertklep
  • 198,204
  • 35
  • 394
  • 381