I want to flush the winston logger before process.exit
.
process.on('uncaughtException', function(err){
logger.error('Fatal uncaught exception crashed cluster', err);
logger.flush(function(){ // <-
process.exit(1);
});
});
Is there anything like logger.flush
available? I couldn't find anything about it, other than people complaining about winston not being very actively maintained.
As an alternative, is there any popular (actively maintained) multi-transport logging framework that provides a flushing capability?
Calling
process.exit
inside the log-callback like Thomas Heymann suggested will not ensure that the logs are actually flushed, especially when using aFile
-transport.Instead of calling process.exit directly I would let the logger call
process.exit
after the log was flushed:logger.js :
And in your code:
Tested on NodeJS v4.1.2 and winston 1.1.0
Winston has a better way to deal with this exceptions.
Winston actually allows you to pass in a callback which is executed when all transports have been logged:
Docs: https://github.com/flatiron/winston#events-and-callbacks-in-winston
Unfortuantely, Winston will sometimes call the logging callback before the transport has had a chance to flush, so the accepted answer can still lead to un-saved log messages (especially on the first turn of the event loop). A better solution is implemented in the winston-log-and-exit package / patch.