Search code examples
node.jswinston

How to flush winston logs?


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?


Solution

  • Winston actually allows you to pass in a callback which is executed when all transports have been logged:

    process.on('uncaughtException', function(err) {
        logger.log('error', 'Fatal uncaught exception crashed cluster', err, function(err, level, msg, meta) {
            process.exit(1);
        });
    });
    

    Docs: https://github.com/flatiron/winston#events-and-callbacks-in-winston