Skip to content

Commit 89f3614

Browse files
committed
[fix] Logging now survives child process restarts.
1 parent 2d7d462 commit 89f3614

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

lib/forever/plugins/logger.js

+34-19
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,44 @@ exports.attach = function (options) {
77
options = options || {};
88
var monitor = this;
99

10-
monitor.on('start', function startLogs(child, childData) {
10+
11+
if (options.outFile) {
12+
monitor.stdout = options.stdout || fs.createWriteStream(options.outFile, {
13+
flags: monitor.append ? 'a+' : 'w+',
14+
encoding: 'utf8',
15+
mode: 0644
16+
});
17+
}
18+
19+
if (options.errFile) {
20+
monitor.stderr = options.stderr || fs.createWriteStream(options.errFile, {
21+
flags: monitor.append ? 'a+' : 'w+',
22+
encoding: 'utf8',
23+
mode: 0644
24+
});
25+
}
26+
27+
monitor.on('start', startLogs);
28+
29+
monitor.on('restart', startLogs);
30+
31+
monitor.on('exit', function () {
32+
monitor.stdout.end();
33+
monitor.stderr.end();
34+
});
35+
36+
function startLogs(child, childData) {
1137
if (monitor.child && !monitor.silent) {
12-
monitor.child.stdout.pipe(process.stdout);
13-
monitor.child.stderr.pipe(process.stderr);
14-
if (monitor.outFile) {
15-
monitor.stdout = fs.createWriteStream(monitor.outFile, {
16-
flags: monitor.append ? 'a+' : 'w+',
17-
encoding: 'utf8',
18-
mode: 0644
19-
});
20-
monitor.child.stdout.pipe(monitor.stdout);
38+
monitor.child.stdout.pipe(process.stdout, { end: false });
39+
monitor.child.stderr.pipe(process.stderr, { end: false });
40+
if (monitor.stdout) {
41+
monitor.child.stdout.pipe(monitor.stdout, { end: false });
2142
}
22-
if (monitor.errFile) {
23-
monitor.stderr = fs.createWriteStream(monitor.errFile, {
24-
flags: monitor.append ? 'a+' : 'w+',
25-
encoding: 'utf8',
26-
mode: 0644
27-
});
28-
monitor.child.stderr.pipe(monitor.stderr);
43+
if (monitor.stderr) {
44+
monitor.child.stderr.pipe(monitor.stderr, { end: false });
2945
}
3046
}
31-
});
32-
47+
}
3348
};
3449

3550

0 commit comments

Comments
 (0)