diff --git a/lib/util/logger.js b/lib/util/logger.js index 57513fdf..d455ca1e 100644 --- a/lib/util/logger.js +++ b/lib/util/logger.js @@ -1,4 +1,6 @@ var util = require('util') +var events = require('events') + var colors = require('colors') function Log(tag, stream) { @@ -20,6 +22,18 @@ function Log(tag, stream) { , FTL: 'red' } this.localIdentifier = null + events.EventEmitter.call(this) +} + +util.inherits(Log, events.EventEmitter) + +Log.Entry = function(priority, tag, pid, identifier, message) { + this.time = Date.now() + this.priority = priority + this.tag = tag + this.pid = pid + this.identifier = identifier + this.message = message } Log.prototype.setLocalIdentifier = function(identifier) { @@ -27,45 +41,56 @@ Log.prototype.setLocalIdentifier = function(identifier) { } Log.prototype.debug = function() { - this._write(this._format(this.levels.DEBUG, arguments)) + this._write(this._entry(this.levels.DEBUG, arguments)) } Log.prototype.verbose = function() { - this._write(this._format(this.levels.VERBOSE, arguments)) + this._write(this._entry(this.levels.VERBOSE, arguments)) } Log.prototype.info = function() { - this._write(this._format(this.levels.INFO, arguments)) + this._write(this._entry(this.levels.INFO, arguments)) } Log.prototype.warn = function() { - this._write(this._format(this.levels.WARNING, arguments)) + this._write(this._entry(this.levels.WARNING, arguments)) } Log.prototype.error = function() { - this._write(this._format(this.levels.ERROR, arguments)) + this._write(this._entry(this.levels.ERROR, arguments)) } Log.prototype.fatal = function() { - this._write(this._format(this.levels.FATAL, arguments)) + this._write(this._entry(this.levels.FATAL, arguments)) } Log.prototype._color = function(priority) { return priority[this.colors[priority]] } -Log.prototype._format = function(priority, args) { - return util.format('%s/%s %d [%s] %s' - , this._color(priority) - , this.tag - , process.pid - , this.localIdentifier || Log.globalIdentifier - , util.format.apply(util, args) +Log.prototype._entry = function(priority, args) { + return new Log.Entry( + priority + , this.tag + , process.pid + , this.localIdentifier || Log.globalIdentifier + , util.format.apply(util, args) ) } -Log.prototype._write = function(out) { - console.error(out) +Log.prototype._format = function(entry) { + return util.format('%s/%s %d [%s] %s' + , this._color(entry.priority) + , entry.tag + , entry.pid + , entry.identifier + , entry.message + ) +} + +Log.prototype._write = function(entry) { + console.error(this._format(entry)) + this.emit('entry', entry) } Log.globalIdentifier = '*'