nomiddlename/log4js-node — pull request #149 by mkielar (2013-08-08 22:24:26+00:00)
Title: Clustered appender for log4js.

Hi,

I've created a multiprocess appender that uses process.send / worker.on(message, callback) mechanisms instead of sockets in order to synchronize logging.

It also allows use of multiple _actual_ loggers in the master logger, and does not require user/programmer do point explicitely which _mode_ to use.

See an example below:

```javascript
if (cluster.isMaster) {

// Init master logger
log4js.configure({
appenders: [
{
// Adding only one, "clustered" appender.
// Note, there is no "mode" setting - the mode (master / worker) is automatucally determined from cluster.isMaster / cluster.isWorker
type: "clustered",

// Add as many "normal" appenders as you like. They wil all be used once the "clustered" master appender receives a loggingEvent
appenders: [
{ type: 'console' },
{ type: 'file', filename: "log.log", maxLogSize: 10000000, backups: 10 },
]
}
]
});

// Init logger like you used to
var logger = log4js.getLogger("master");

// Fork workers.
for (var i = 0; i < 4; i++) {
var worker = cluster.fork();
logger.info("forked: " + i);
}

} else {

// Init worker loggers
log4js.configure({
appenders: [
{
// Node, there again is no "mode" setting as the worker mode is automatically determined from cluster.isWorker
type: "clustered",

// Note, we do not add any other appenders here. Every message is sent to master logger.
}
]
});

// Init logger like you used to
var logger = log4js.getLogger("worker_" + cluster.worker.id);

logger.info('message: ' + i + ' from worker ' + cluster.worker.id);

}