Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 269
bevacqua Avatar asked Sep 12 '13 18:09

bevacqua


4 Answers

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

like image 119
Thomas Heymann Avatar answered Oct 18 '22 07:10

Thomas Heymann


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.

like image 26
Jthorpe Avatar answered Oct 18 '22 07:10

Jthorpe


Calling process.exit inside the log-callback like Thomas Heymann suggested will not ensure that the logs are actually flushed, especially when using a File-transport.

Instead of calling process.exit directly I would let the logger call process.exit after the log was flushed:

logger.js :

var winston = require('winston');

winston.loggers.add('my-logger', {
    console: {
        level: 'debug',
        colorize: true,
        timestamp: true,
        handleExceptions: true
    },
    file: {
        level: 'info',
        colorize: false,
        timestamp: true,
        filename: file,
        handleExceptions: true
    }
});

var logger = winston.loggers.get('my-logger');


/* ******* *
 * EXPORTS
 * ******* */

exports.exitAfterFlush = function(code) {
    logger.transports.file.on('flush', function() {
        process.exit(code);
    });
};

exports.info = function() {
    logger.info.apply(this, arguments);
};

exports.warn = function() {
    logger.info.apply(this, arguments);
};

exports.error = function() {
    logger.info.apply(this, arguments);
};

And in your code:

var logger = require('./logger.js');
logger.exitAfterFlush(0);
info('Done!');

Tested on NodeJS v4.1.2 and winston 1.1.0

like image 1
Biggie Avatar answered Oct 18 '22 06:10

Biggie


This may help someone. logger is an instance of winston.createLogger which defines two transports (console & file). The exit code is reflected in the shell.

const logger = require('../library/log');

function exitAfterFlush() {
    logger.on('finish', function () {
        logger.end();
    });
};
  
process.on('exit', (code) => {
    console.log(`About to exit with code: ${code}`);
});

logger.info(`Started with PID:${process.pid}`);
logger.error(`*****`);
process.exitCode = 11;
exitAfterFlush();
like image 1
300baud Avatar answered Oct 18 '22 07:10

300baud