Search code examples
node.jswinston

Winston logging not working when using cluster


I am currently starting a cluster app and the logging doesn't seem to output. I am using an external reference for winston so that I can apply a similar log in any nodes I create, but even the basic console transport isnt working. If I replace the winston calls with the system "console.log" calls, the output works just fine. Not sure where I am going wrong with this:

logger.js

var winston = require('winston'),
    logger,
    customLevels = {workr: 0, info: 1, debug: 2, error: 3};
winston.emitErrs = true;
logger = new winston.Logger({levels: customLevels, exitOnError: false});
logger.enableConsole = function () {
    logger.add (winston.transports.Console, {
        name: 'console',
        formatter: function (options) {
            return options.level.toUpperCase() + ' - ' + options.message;
        }
    });
};
//I am omitting a file transport here
module.exports = logger;
module.exports.stream = {
    write: function(message, encoding){
        logger.info(message);
    }
};

cluster.js

var cluster = require('cluster'),
    logger = require('./logger'),
    maxJobs = Math.floor(require('os').cpus().length / 2),
    activeJobs = 0;

if (cluster.isMaster) {
    logger.enableConsole();
    cluster.setupMaster({silent: true});

    logger.log('workr', 'MaxJobs: ' + maxJobs);
    setInterval(function () {
        logger.log('workr', "Active: " + activeJobs);
    }, 200);

    for (var i = 0; i < maxJobs; i++) {
        cluster.fork();
    }

    Object.keys(cluster.workers).forEach(function (id) {
        cluster.workers[id].process.stdout.on('data', function (chunk) {
            logger.log('workr', chunk);
        });
        cluster.workers[id].process.stderr.on('data', function (chunk) {
            logger.log('error', 'Error in worker ' + id + ': ' + chunk);
        });
        cluster.workers[id].on('message', function (msg) {
            if (msg.update !== undefined) {
                activeJobs += msg.update ? 1 : -1;
            }
        });
    })

    cluster.on('exit', function (worker, code, signal) {
        logger.log('workr', 'worker ' + worker.process.pid + ' died');
    });
} else {
    setTimeout(function () {
        console.log('Adding 1');
        process.send({ update: true });
        setTimeout(function () {
            console.log('Removing 1');
            process.send({ update: false });            
        }, Math.random() * 500 + 1000);
    }, Math.random() * 600 + 200);
}

Solution

  • This turned out to be an issue with my Winston implementation. I was not specifying the logging level in Winston, which meant it defaulted to 'info' and this was a higher level than my 'workr' level. By adding the specific level it seems to be working now.

    ...
    logger.enableConsole = function () {
        logger.add (winston.transports.Console, {
            name: 'console',
            formatter: function (options) {
                return options.level.toUpperCase() + ' - ' + options.message;
            },
            level: 'workr' // This was what I was missing
        });
    };