Skip to content

Commit

Permalink
[refactor] Add Logger plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalecki committed Dec 9, 2011
1 parent ab0f8e9 commit f84634b
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions lib/forever/plugins/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var Logger = module.exports;

Logger.name = 'logger';

Logger.attach = function (options) {
var self = this;

this.on('start', function () {
startLogging('stdout');
startLogging('stderr');

function startLogging(stream) {
self.child[stream].on('data', onData);

if (options[stream]) {
self[stream] = options[stream]
}

function onData(data) {
if (!self.silent && !self[stream]) {
//
// If we haven't been silenced, and we don't have a file stream
// to output to write to the process stdout stream
//
process[stream].write(data);
}
else if (self[stream]) {
//
// If we have been given an output file for the stream, write to it
//
self[stream].write(data);
}
}

function stopLogging() {
if (self[stream] && !options[stream]) {
//
// Close the stream only if it wasn't provided in `options`. If it was,
// caller is responsible for closing it.
//
self[stream].close();
}
};

self.on('exit', stopLogging);
}
});
};

Logger.init = function (done) {
done();
};

0 comments on commit f84634b

Please sign in to comment.