mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-10-03 20:20:35 +00:00
Correctly set log level
This commit is contained in:
@@ -36,6 +36,10 @@ function encodeGlobal(output) {
|
||||
}
|
||||
};
|
||||
|
||||
result.isMultilingual = function() {
|
||||
return book.isMultilingual();
|
||||
};
|
||||
|
||||
result.isLanguageBook = function() {
|
||||
return false;
|
||||
};
|
||||
|
||||
+1
-1
@@ -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;
|
||||
|
||||
@@ -161,6 +161,18 @@ Book.prototype.isMultilingual = function() {
|
||||
return (this.getLanguages().getList().size > 0);
|
||||
};
|
||||
|
||||
/**
|
||||
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
|
||||
|
||||
|
||||
+40
-16
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user