diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-04-26 20:49:20 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-04-26 20:49:20 +0200 |
commit | 7b7e4f60bc12e01d397487424635a9426990884c (patch) | |
tree | 370e0e3124fc34b031e4dab285e5131ef99c8643 /lib/utils | |
parent | 8bac111acf121adf03cc2dff09bd0a1ce8cea19b (diff) | |
download | gitbook-7b7e4f60bc12e01d397487424635a9426990884c.zip gitbook-7b7e4f60bc12e01d397487424635a9426990884c.tar.gz gitbook-7b7e4f60bc12e01d397487424635a9426990884c.tar.bz2 |
Correctly set log level
Diffstat (limited to 'lib/utils')
-rw-r--r-- | lib/utils/logger.js | 56 |
1 files changed, 40 insertions, 16 deletions
diff --git a/lib/utils/logger.js b/lib/utils/logger.js index 60215af..a913709 100644 --- a/lib/utils/logger.js +++ b/lib/utils/logger.js @@ -17,14 +17,13 @@ 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.lastChar = '\n'; - // Define log level - this.setLevel(logLevel); + this.setLevel(logLevel || 'info'); _.bindAll(this); @@ -40,35 +39,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 +95,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 +106,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 +119,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; |