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 | |
parent | 8bac111acf121adf03cc2dff09bd0a1ce8cea19b (diff) | |
download | gitbook-7b7e4f60bc12e01d397487424635a9426990884c.zip gitbook-7b7e4f60bc12e01d397487424635a9426990884c.tar.gz gitbook-7b7e4f60bc12e01d397487424635a9426990884c.tar.bz2 |
Correctly set log level
-rw-r--r-- | lib/api/encodeGlobal.js | 4 | ||||
-rw-r--r-- | lib/cli/getBook.js | 2 | ||||
-rw-r--r-- | lib/models/book.js | 12 | ||||
-rw-r--r-- | lib/utils/logger.js | 56 |
4 files changed, 57 insertions, 17 deletions
diff --git a/lib/api/encodeGlobal.js b/lib/api/encodeGlobal.js index b2dd77a..05e9384 100644 --- a/lib/api/encodeGlobal.js +++ b/lib/api/encodeGlobal.js @@ -36,6 +36,10 @@ function encodeGlobal(output) { } }; + result.isMultilingual = function() { + return book.isMultilingual(); + }; + result.isLanguageBook = function() { return false; }; diff --git a/lib/cli/getBook.js b/lib/cli/getBook.js index 5c29078..9038669 100644 --- a/lib/cli/getBook.js +++ b/lib/cli/getBook.js @@ -17,7 +17,7 @@ function getBook(args, kwargs) { var fs = createNodeFS(input); var book = Book.createForFS(fs); - return book; + return Book.setLogLevel(book, logLevel); } module.exports = getBook; diff --git a/lib/models/book.js b/lib/models/book.js index eb8011b..3c5144f 100644 --- a/lib/models/book.js +++ b/lib/models/book.js @@ -162,6 +162,18 @@ Book.prototype.isMultilingual = function() { }; /** + Change log level + + @param {Book} book + @param {String} level + @return {Book} +*/ +Book.setLogLevel = function setLogLevel(book, level) { + book.getLogger().setLevel(level); + return book; +}; + +/** Create a book using a filesystem @param {FS} fs 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; |