diff --git a/lib/winston/config.js b/lib/winston/config.js index 7c3c705b3..bc55c1b87 100644 --- a/lib/winston/config.js +++ b/lib/winston/config.js @@ -23,15 +23,18 @@ config.colorize = function (level, message) { for (var i = 0, l = allColors[level].length; i < l; ++i) { colorized = colors[allColors[level][i]](colorized); } - } else if (allColors[level].match(/\s/)) { + } + else if (allColors[level].match(/\s/)) { var colorArr = allColors[level].split(/\s+/); for (var i = 0; i < colorArr.length; ++i) { colorized = colors[colorArr[i]](colorized); } allColors[level] = colorArr; - } else { + } + else { colorized = colors[allColors[level]](colorized); } + return colorized; }; diff --git a/lib/winston/logger.js b/lib/winston/logger.js index 363bf9142..ea259a634 100755 --- a/lib/winston/logger.js +++ b/lib/winston/logger.js @@ -512,26 +512,19 @@ Logger.prototype.remove = function (transport) { return this; }; -var ProfileHandler = function (logger) { - this.logger = logger; - - this.start = Date.now(); - - this.done = function (msg) { - var args, callback, meta; - args = Array.prototype.slice.call(arguments); - callback = typeof args[args.length - 1] === 'function' ? args.pop() : null; - meta = typeof args[args.length - 1] === 'object' ? args.pop() : {}; - - meta.durationMs = (Date.now()) - this.start; - - return this.logger.info(msg, meta, callback); - } -} - +// +// ### function startTimer () +// Returns an object corresponding to a specific timing. When done +// is called the timer will finish and log the duration. e.g.: +// +// timer = winston.startTimer() +// setTimeout(function(){ +// timer.done("Logging message"); +// }, 1000); +// Logger.prototype.startTimer = function () { return new ProfileHandler(this); -} +}; // // ### function profile (id, [msg, meta, callback]) @@ -674,3 +667,28 @@ Logger.prototype._onError = function (transport, err) { this.emit('error', err, transport); } }; + +// +// ### @private ProfileHandler +// Constructor function for the ProfileHandler instance used by +// `Logger.prototype.startTimer`. When done is called the timer +// will finish and log the duration. +// +function ProfileHandler(logger) { + this.logger = logger; + this.start = Date.now(); +} + +// +// ### function done (msg) +// Ends the current timer (i.e. ProfileHandler) instance and +// logs the `msg` along with the duration since creation. +// +ProfileHandler.prototype.done = function (msg) { + var args = Array.prototype.slice.call(arguments), + callback = typeof args[args.length - 1] === 'function' ? args.pop() : null, + meta = typeof args[args.length - 1] === 'object' ? args.pop() : {}; + + meta.duration = (Date.now()) - this.start + 'ms'; + return this.logger.info(msg, meta, callback); +};