Skip to content

Commit 7c0c3b8

Browse files
committed
[api] Refactor to use winston instead of pure sys.puts() for logging
1 parent cc3d465 commit 7c0c3b8

File tree

3 files changed

+54
-41
lines changed

3 files changed

+54
-41
lines changed

bin/forever

+43-26
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var path = require('path'),
44
fs = require('fs'),
55
eyes = require('eyes'),
6+
winston = require('winston'),
67
sys = require('sys');
78

89
var accepts = ['start', 'stop', 'stopall', 'list', 'cleanlogs', 'restart'], action;
@@ -34,6 +35,7 @@ var help = [
3435
" -d SOURCEDIR The source directory for which SCRIPT is relative to",
3536
" -p PATH Base path for all forever related files (pid files, etc.)",
3637
" -c COMMAND COMMAND to execute (defaults to node)",
38+
" -v, --verbose Turns on the verbose messages from Forever",
3739
" -s, --silent Run the child script silencing stdout and stderr",
3840
" -h, --help You're staring at it",
3941
"",
@@ -51,15 +53,17 @@ var help = [
5153
].join('\n');
5254

5355
var mappings = {
54-
'm': 'max',
55-
'l': 'logFile',
56-
'p': 'path',
57-
'c': 'command',
58-
'd': 'sourceDir',
59-
's': 'silent',
60-
'silent': 'silent',
61-
'o': 'outFile',
62-
'e': 'errFile'
56+
'c': 'command',
57+
'e': 'errFile',
58+
'd': 'sourceDir',
59+
'l': 'logFile',
60+
'm': 'max',
61+
'o': 'outFile',
62+
'p': 'path',
63+
's': 'silent',
64+
'silent': 'silent',
65+
'v': 'verbose',
66+
'verbose': 'verbose'
6367
};
6468

6569
function isSimpleAction () {
@@ -101,6 +105,15 @@ if (typeof options['max'] === 'undefined') {
101105
// restarting outside of the main directory
102106
if (!options.sourceDir) options.sourceDir = process.cwd();
103107

108+
//
109+
// Configure winston for forever based on the CLI options
110+
//
111+
winston.defaultTransports().console.timestamp = false;
112+
winston.defaultTransports().console.colorize = true;
113+
if (options.verbose) {
114+
winston.defaultTransports().console.level = 'silly';
115+
}
116+
104117
// Setup configurations for forever
105118
var config = {
106119
root: argv.p
@@ -116,7 +129,7 @@ function tryStart (callback) {
116129

117130
forever.stat(fullLog, fullScript, function (err) {
118131
if (err) {
119-
sys.puts('Cannot start forever: ' + err.message);
132+
winston.error('Cannot start forever: ' + err.message);
120133
process.exit(0);
121134
}
122135

@@ -129,28 +142,26 @@ function tryStart (callback) {
129142
// the default root exposed by forever.
130143
//
131144
if (config.root && config.root !== forever.root) {
132-
sys.puts('Loading forever with config: ');
133-
eyes.inspect(config);
145+
winston.silly('Loading forever with config: ', config);
134146

135147
forever.load(config);
136-
sys.puts('Loaded forever successfully.');
148+
winston.silly('Loaded forever successfully.');
137149
}
138150

139-
sys.puts('Tidying ' + forever.config.root);
151+
winston.silly('Tidying ' + forever.config.root);
140152
var tidy = forever.cleanUp(action === 'cleanlogs');
141153
tidy.on('cleanUp', function () {
142-
sys.puts(forever.config.root + ' tidied.');
154+
winston.silly(forever.config.root + ' tidied.');
143155
if (file) {
144-
sys.puts('Working with file: ' + file);
156+
winston.info('Forever processing arguments', { arg: file });
145157
}
146158

147159
if (options) {
148-
sys.puts('Using forever options:');
149-
eyes.inspect(options);
160+
winston.silly('Forever using options', options);
150161
}
151162

152163
if (action) {
153-
sys.puts('Running action: ' + action.yellow);
164+
winston.info('Running action: ' + action.yellow);
154165
switch (action) {
155166
case 'start':
156167
tryStart(function () { forever.startDaemon(file, options); });
@@ -159,23 +170,23 @@ tidy.on('cleanUp', function () {
159170
case 'stop':
160171
var runner = forever.stop(file, true);
161172
runner.on('stop', function (process) {
162-
sys.puts('Forever stopped process:');
173+
winston.info('Forever stopped process:');
163174
sys.puts(process);
164175
});
165176
runner.on('error', function (err) {
166-
sys.puts('Forever cannot find process with index: ' + file)
177+
winston.error('Forever cannot find process with index: ' + file)
167178
})
168179
break;
169180

170181
case 'stopall':
171182
var runner = forever.stopAll(true);
172183
runner.on('stopAll', function (processes) {
173184
if (processes) {
174-
sys.puts('Forever stopped processes:');
185+
winston.info('Forever stopped processes:');
175186
sys.puts(processes);
176187
}
177188
else {
178-
sys.puts('No forever processes running');
189+
winston.info('No forever processes running');
179190
}
180191
});
181192
break;
@@ -184,18 +195,24 @@ tidy.on('cleanUp', function () {
184195
var runner = forever.restart(file, true);
185196
runner.on('restart', function (processes) {
186197
if (processes) {
187-
sys.puts('Forever restarted processes:');
198+
winston.info('Forever restarted processes:');
188199
sys.puts(processes);
189200
}
190201
else {
191-
sys.puts('No forever processes running');
202+
winston.info('No forever processes running');
192203
}
193204
});
194205
break;
195206

196207
case 'list':
197208
var processes = forever.list(true);
198-
sys.puts(processes ? processes : 'No forever processes running');
209+
if (processes) {
210+
winston.info('Forever processes running');
211+
sys.puts(processes);
212+
}
213+
else {
214+
winston.info('No forever processes running');
215+
}
199216
break;
200217
}
201218
}

lib/forever/monitor.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var sys = require('sys'),
1111
path = require('path'),
1212
events = require('events'),
1313
spawn = require('child_process').spawn,
14+
winston = require('winston'),
1415
forever = require('forever');
1516

1617
//
@@ -32,6 +33,12 @@ var Monitor = exports.Monitor = function (script, options) {
3233
this.pidFile = options.pidFile;
3334
this.outFile = options.outFile;
3435
this.errFile = options.errFile;
36+
this.logger = options.logger || new (winston.Logger)({
37+
transports: [new winston.transports.Console()]
38+
});
39+
40+
// Extend from the winston logger.
41+
this.logger.extend(this);
3542

3643
this.childExists = false;
3744

@@ -113,12 +120,12 @@ Monitor.prototype.start = function (restart) {
113120
listenTo('stderr');
114121

115122
child.on('exit', function (code) {
116-
self.log('Forever detected script exited with code: ' + code);
123+
self.error('Forever detected script exited with code: ' + code);
117124
self.times++;
118125

119126
if ((self.forever || self.times < self.max) && !self.forceStop) {
120127
process.nextTick(function () {
121-
self.log('Forever restarting script for ' + self.times + ' time');
128+
self.warn('Forever restarting script for ' + self.times + ' time');
122129
self.start(true);
123130
});
124131
}
@@ -216,18 +223,6 @@ Monitor.prototype.save = function () {
216223
return this;
217224
};
218225

219-
//
220-
// ### function log (message)
221-
// #### @message {string} String to log.
222-
// Utility function for logging forever actions
223-
//
224-
Monitor.prototype.log = function (message) {
225-
if (!this.silent) {
226-
sys.puts(message);
227-
}
228-
return this;
229-
};
230-
231226
//
232227
// ### function restart ()
233228
// Restarts the target script associated with this instance.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"daemon": ">= 0.3.0",
1919
"optimist": ">= 0.0.6",
2020
"timespan": ">= 2.0.0",
21-
"vows": ">= 0.5.2"
21+
"vows": ">= 0.5.2",
22+
"winston": ">= 0.2.1"
2223
},
2324
"bin": { "forever": "./bin/forever" },
2425
"main": "./lib/forever",

0 commit comments

Comments
 (0)