Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does winston-cluster support winston.containers? #4

Open
johnmcilwain opened this issue Nov 25, 2016 · 2 comments
Open

Does winston-cluster support winston.containers? #4

johnmcilwain opened this issue Nov 25, 2016 · 2 comments

Comments

@johnmcilwain
Copy link

I am probably doing something dumb, but I can't get it to work. My sample code:

var cluster = require('cluster');
var winston = require('winston');
var winstonCluster = require('winston-cluster');

 
if (cluster.isMaster) {
 
    winston.loggers.add('logwinston', {
        console: { 'timestamp': true, colorize: true, label: 'category one' },
        file: { filename: 'logfile1' }
    });
    winston.loggers.add('logsystem', {
        console: { 'timestamp': false, colorize: false, label: 'category two' },
        file: { filename: 'logfile2' }
    });
    
    var category1 = winston.loggers.get('logwinston');
    category1.log('info', 'Hello distributed log files!');

    var category2 = winston.loggers.get('logsystem');
    category2.log('info', 'Hello distributed log files!');

    //Bind logging listeners to workers 
    winstonCluster.bindListeners();

    //Start workers 
    var cpuCount = require('os').cpus().length;
    for (var i = 0; i < cpuCount; i++) {
        cluster.fork();
    }
 
    //Master logic here 
    //... 
 
} else {
 
    //Replace default transport with cluster transport 
    winstonCluster.bindTransport();

    var category1 = winston.loggers.get('logwinston');
    category1.log('info', 'Child here');

    var category2 = winston.loggers.get('logsystem');
    category2.log('info', 'Child here');

 
    //Slave logic here 
    //... 
 
}

@ryankurte
Copy link
Owner

Hey @johnmcilwain,

With respect to your email, and issue #1, if winston works clustered now you may not need this. At the time that I wrote it, it didn't, and I needed to make sure logging would be handled by only one process (particularly for file writing), which it has been doing in production for a year or so now.

As it stands, it only overwrites the transports for the default handler, so does not currently support containers / instances.

You can see on line 70 how the master passes log events to the default winston instance, and on line 84 how the child transport is overwritten to use the cluster IPC.

If you need containers / instances you could probably alter the bindTransport function to take an optional winston instance to overwrite the underlying transport for a provided instance instead of the default instance, though I don't know what other magic occurs under the hood of winston.

Something like:

var bindTransport = exports.bindTransport = function(instance) {
    if(typeof instance !== 'undefined') {
       instance.remove(winston.transports.Console);
       instance.add(winston.transports.Cluster, {});
    } else {
        winston.remove(winston.transports.Console);
        winston.add(winston.transports.Cluster, {});
    }
}

Then in the child process:

var lw = winston.loggers.get('logwinston');
winstonCluster.bindTransport(lw);

Might work, though would route all messages to the default handler in the parent.

If you want these to pass back to separate instances in the parent, an additional field (instance, ie. 'logwinston') may need to be passed through the IPC and used to select which instance to write back to.

As an aside from that, I believe you will need to instantiate the loggers in both the parent and child processes (var category1 = winston.loggers.get('logwinston');).

If you decide to implement any of this, I would welcome a pull request!

Good luck,

Ryan

@johnmcilwain
Copy link
Author

johnmcilwain commented Nov 29, 2016 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants