I’m trying to use node core’s cluster feature.
I would like the stdout and stderr streams to output to a file, one for each worker id.
Something much like the following:
var fs = require('fs'),
env = process.env,
workerId = env.NODE_WORKER_ID || env.NODE_UNIQUE_ID;
process.stdout = fs.createWriteStream(__dirname + '/app#' + workerId + '.log', {
encoding: 'utf8'
});
Unfortunately, it does not seem to rewrite process.stdout like this.
Is there a way to achieve this, or should this be done differently? Currently when I run my cluster I am getting all output from all processes in one console, which is extremely messy.
I ended up doing the following:
It may not be very elegant, but so far this is the best solution I’ve found.