diff --git a/lib/forever/plugins/logger.js b/lib/forever/plugins/logger.js index b6b54a00..17772f6c 100644 --- a/lib/forever/plugins/logger.js +++ b/lib/forever/plugins/logger.js @@ -7,29 +7,44 @@ exports.attach = function (options) { options = options || {}; var monitor = this; - monitor.on('start', function startLogs(child, childData) { + + if (options.outFile) { + monitor.stdout = options.stdout || fs.createWriteStream(options.outFile, { + flags: monitor.append ? 'a+' : 'w+', + encoding: 'utf8', + mode: 0644 + }); + } + + if (options.errFile) { + monitor.stderr = options.stderr || fs.createWriteStream(options.errFile, { + flags: monitor.append ? 'a+' : 'w+', + encoding: 'utf8', + mode: 0644 + }); + } + + monitor.on('start', startLogs); + + monitor.on('restart', startLogs); + + monitor.on('exit', function () { + monitor.stdout.end(); + monitor.stderr.end(); + }); + + function startLogs(child, childData) { if (monitor.child && !monitor.silent) { - monitor.child.stdout.pipe(process.stdout); - monitor.child.stderr.pipe(process.stderr); - if (monitor.outFile) { - monitor.stdout = fs.createWriteStream(monitor.outFile, { - flags: monitor.append ? 'a+' : 'w+', - encoding: 'utf8', - mode: 0644 - }); - monitor.child.stdout.pipe(monitor.stdout); + monitor.child.stdout.pipe(process.stdout, { end: false }); + monitor.child.stderr.pipe(process.stderr, { end: false }); + if (monitor.stdout) { + monitor.child.stdout.pipe(monitor.stdout, { end: false }); } - if (monitor.errFile) { - monitor.stderr = fs.createWriteStream(monitor.errFile, { - flags: monitor.append ? 'a+' : 'w+', - encoding: 'utf8', - mode: 0644 - }); - monitor.child.stderr.pipe(monitor.stderr); + if (monitor.stderr) { + monitor.child.stderr.pipe(monitor.stderr, { end: false }); } } - }); - + } };