Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50655 views
1
/*
2
* logger.js: Plugin for `Monitor` instances which adds stdout and stderr logging.
3
*
4
* (C) 2010 Nodejitsu Inc.
5
* MIT LICENCE
6
*
7
*/
8
9
var fs = require('fs');
10
11
//
12
// Name the plugin
13
//
14
exports.name = 'logger';
15
16
//
17
// ### function attach (options)
18
// #### @options {Object} Options for attaching to `Monitor`
19
//
20
// Attaches functionality for logging stdout and stderr to `Monitor` instances.
21
//
22
exports.attach = function (options) {
23
options = options || {};
24
var monitor = this;
25
26
if (options.outFile) {
27
monitor.stdout = options.stdout || fs.createWriteStream(options.outFile, {
28
flags: monitor.append ? 'a+' : 'w+',
29
encoding: 'utf8',
30
mode: 0644
31
});
32
}
33
34
if (options.errFile) {
35
monitor.stderr = options.stderr || fs.createWriteStream(options.errFile, {
36
flags: monitor.append ? 'a+' : 'w+',
37
encoding: 'utf8',
38
mode: 0644
39
});
40
}
41
42
monitor.on('start', startLogs);
43
monitor.on('restart', startLogs);
44
monitor.on('exit', function () {
45
if (monitor.stdout) {
46
monitor.stdout.destroySoon();
47
}
48
49
if (monitor.stderr) {
50
monitor.stderr.destroySoon();
51
}
52
});
53
54
function startLogs(child, childData) {
55
if (monitor.child) {
56
monitor.child.stdout.on('data', function onStdout(data) {
57
monitor.emit('stdout', data);
58
});
59
60
monitor.child.stderr.on('data', function onStderr(data) {
61
monitor.emit('stderr', data);
62
});
63
64
if (!monitor.silent) {
65
monitor.child.stdout.pipe(process.stdout, { end: false });
66
monitor.child.stderr.pipe(process.stderr, { end: false });
67
}
68
69
if (monitor.stdout) {
70
monitor.child.stdout.pipe(monitor.stdout, { end: false });
71
}
72
73
if (monitor.stderr) {
74
monitor.child.stderr.pipe(monitor.stderr, { end: false });
75
}
76
}
77
}
78
};
79
80
81
82