From f84634bb593c3e796dc5cba325460e406a512256 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Ma=C5=82ecki?= Date: Mon, 5 Dec 2011 19:42:31 +0100 Subject: [PATCH] [refactor] Add `Logger` plugin --- lib/forever/plugins/logger.js | 53 +++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 lib/forever/plugins/logger.js diff --git a/lib/forever/plugins/logger.js b/lib/forever/plugins/logger.js new file mode 100644 index 00000000..dd29af6c --- /dev/null +++ b/lib/forever/plugins/logger.js @@ -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(); +}; +