Skip to content

Commit

Permalink
[api] Refactor to use winston instead of pure sys.puts() for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Feb 15, 2011
1 parent cc3d465 commit 7c0c3b8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 41 deletions.
69 changes: 43 additions & 26 deletions bin/forever
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var path = require('path'),
fs = require('fs'),
eyes = require('eyes'),
winston = require('winston'),
sys = require('sys');

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

var mappings = {
'm': 'max',
'l': 'logFile',
'p': 'path',
'c': 'command',
'd': 'sourceDir',
's': 'silent',
'silent': 'silent',
'o': 'outFile',
'e': 'errFile'
'c': 'command',
'e': 'errFile',
'd': 'sourceDir',
'l': 'logFile',
'm': 'max',
'o': 'outFile',
'p': 'path',
's': 'silent',
'silent': 'silent',
'v': 'verbose',
'verbose': 'verbose'
};

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

//
// Configure winston for forever based on the CLI options
//
winston.defaultTransports().console.timestamp = false;
winston.defaultTransports().console.colorize = true;
if (options.verbose) {
winston.defaultTransports().console.level = 'silly';
}

// Setup configurations for forever
var config = {
root: argv.p
Expand All @@ -116,7 +129,7 @@ function tryStart (callback) {

forever.stat(fullLog, fullScript, function (err) {
if (err) {
sys.puts('Cannot start forever: ' + err.message);
winston.error('Cannot start forever: ' + err.message);
process.exit(0);
}

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

forever.load(config);
sys.puts('Loaded forever successfully.');
winston.silly('Loaded forever successfully.');
}

sys.puts('Tidying ' + forever.config.root);
winston.silly('Tidying ' + forever.config.root);
var tidy = forever.cleanUp(action === 'cleanlogs');
tidy.on('cleanUp', function () {
sys.puts(forever.config.root + ' tidied.');
winston.silly(forever.config.root + ' tidied.');
if (file) {
sys.puts('Working with file: ' + file);
winston.info('Forever processing arguments', { arg: file });
}

if (options) {
sys.puts('Using forever options:');
eyes.inspect(options);
winston.silly('Forever using options', options);
}

if (action) {
sys.puts('Running action: ' + action.yellow);
winston.info('Running action: ' + action.yellow);
switch (action) {
case 'start':
tryStart(function () { forever.startDaemon(file, options); });
Expand All @@ -159,23 +170,23 @@ tidy.on('cleanUp', function () {
case 'stop':
var runner = forever.stop(file, true);
runner.on('stop', function (process) {
sys.puts('Forever stopped process:');
winston.info('Forever stopped process:');
sys.puts(process);
});
runner.on('error', function (err) {
sys.puts('Forever cannot find process with index: ' + file)
winston.error('Forever cannot find process with index: ' + file)
})
break;

case 'stopall':
var runner = forever.stopAll(true);
runner.on('stopAll', function (processes) {
if (processes) {
sys.puts('Forever stopped processes:');
winston.info('Forever stopped processes:');
sys.puts(processes);
}
else {
sys.puts('No forever processes running');
winston.info('No forever processes running');
}
});
break;
Expand All @@ -184,18 +195,24 @@ tidy.on('cleanUp', function () {
var runner = forever.restart(file, true);
runner.on('restart', function (processes) {
if (processes) {
sys.puts('Forever restarted processes:');
winston.info('Forever restarted processes:');
sys.puts(processes);
}
else {
sys.puts('No forever processes running');
winston.info('No forever processes running');
}
});
break;

case 'list':
var processes = forever.list(true);
sys.puts(processes ? processes : 'No forever processes running');
if (processes) {
winston.info('Forever processes running');
sys.puts(processes);
}
else {
winston.info('No forever processes running');
}
break;
}
}
Expand Down
23 changes: 9 additions & 14 deletions lib/forever/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var sys = require('sys'),
path = require('path'),
events = require('events'),
spawn = require('child_process').spawn,
winston = require('winston'),
forever = require('forever');

//
Expand All @@ -32,6 +33,12 @@ var Monitor = exports.Monitor = function (script, options) {
this.pidFile = options.pidFile;
this.outFile = options.outFile;
this.errFile = options.errFile;
this.logger = options.logger || new (winston.Logger)({
transports: [new winston.transports.Console()]
});

// Extend from the winston logger.
this.logger.extend(this);

this.childExists = false;

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

child.on('exit', function (code) {
self.log('Forever detected script exited with code: ' + code);
self.error('Forever detected script exited with code: ' + code);
self.times++;

if ((self.forever || self.times < self.max) && !self.forceStop) {
process.nextTick(function () {
self.log('Forever restarting script for ' + self.times + ' time');
self.warn('Forever restarting script for ' + self.times + ' time');
self.start(true);
});
}
Expand Down Expand Up @@ -216,18 +223,6 @@ Monitor.prototype.save = function () {
return this;
};

//
// ### function log (message)
// #### @message {string} String to log.
// Utility function for logging forever actions
//
Monitor.prototype.log = function (message) {
if (!this.silent) {
sys.puts(message);
}
return this;
};

//
// ### function restart ()
// Restarts the target script associated with this instance.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"daemon": ">= 0.3.0",
"optimist": ">= 0.0.6",
"timespan": ">= 2.0.0",
"vows": ">= 0.5.2"
"vows": ">= 0.5.2",
"winston": ">= 0.2.1"
},
"bin": { "forever": "./bin/forever" },
"main": "./lib/forever",
Expand Down

0 comments on commit 7c0c3b8

Please sign in to comment.