diff options
Diffstat (limited to 'lib/utils/logger.js')
-rw-r--r-- | lib/utils/logger.js | 62 |
1 files changed, 45 insertions, 17 deletions
diff --git a/lib/utils/logger.js b/lib/utils/logger.js index 60215af..fc9c394 100644 --- a/lib/utils/logger.js +++ b/lib/utils/logger.js @@ -17,14 +17,17 @@ var COLORS = { ERROR: color.red }; -function Logger(write, logLevel, prefix) { +function Logger(write, logLevel) { if (!(this instanceof Logger)) return new Logger(write, logLevel); - this._write = write || function(msg) { process.stdout.write(msg); }; + this._write = write || function(msg) { + if(process.stdout) { + process.stdout.write(msg); + } + }; this.lastChar = '\n'; - // Define log level - this.setLevel(logLevel); + this.setLevel(logLevel || 'info'); _.bindAll(this); @@ -40,35 +43,48 @@ function Logger(write, logLevel, prefix) { }, this); } -// Create a new logger prefixed from this logger -Logger.prototype.prefix = function(prefix) { - return (new Logger(this._write, this.logLevel, prefix)); -}; +/** + Change minimum level -// Change minimum level + @param {String} logLevel +*/ Logger.prototype.setLevel = function(logLevel) { if (_.isString(logLevel)) logLevel = LEVELS[logLevel.toUpperCase()]; this.logLevel = logLevel; }; -// Print a simple string +/** + Print a simple string + + @param {String} +*/ Logger.prototype.write = function(msg) { msg = msg.toString(); this.lastChar = _.last(msg); return this._write(msg); }; -// Format a string using the first argument as a printf-like format. +/** + Format a string using the first argument as a printf-like format. +*/ Logger.prototype.format = function() { return util.format.apply(util, arguments); }; -// Print a line +/** + Print a line + + @param {String} +*/ Logger.prototype.writeLn = function(msg) { return this.write((msg || '')+'\n'); }; -// Log/Print a message if level is allowed +/** + Log/Print a message if level is allowed + + @param {Number} level +*/ Logger.prototype.log = function(level) { if (level < this.logLevel) return; @@ -83,7 +99,9 @@ Logger.prototype.log = function(level) { return this.write(msg); }; -// Log/Print a line if level is allowed +/** + Log/Print a line if level is allowed +*/ Logger.prototype.logLn = function() { if (this.lastChar != '\n') this.write('\n'); @@ -92,7 +110,9 @@ Logger.prototype.logLn = function() { return this.log.apply(this, args); }; -// Log a confirmation [OK] +/** + Log a confirmation [OK] +*/ Logger.prototype.ok = function(level) { var args = Array.prototype.slice.apply(arguments, [1]); var msg = this.format.apply(this, args); @@ -103,12 +123,20 @@ Logger.prototype.ok = function(level) { } }; -// Log a "FAIL" +/** + Log a "FAIL" +*/ Logger.prototype.fail = function(level) { return this.log(level, color.red('ERROR') + '\n'); }; -// Log state of a promise +/** + Log state of a promise + + @param {Number} level + @param {Promise} + @return {Promise} +*/ Logger.prototype.promise = function(level, p) { var that = this; |