summaryrefslogtreecommitdiffstats
path: root/lib/utils/logger.js
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-04-30 23:07:43 +0200
committerSamy Pesse <samypesse@gmail.com>2016-04-30 23:07:43 +0200
commitc681cd286a746d5cf3f69fc800bcb79a42e70973 (patch)
tree66b852e90ebfe38da444532af729f7a5af0c6e5d /lib/utils/logger.js
parentc1d53ec11fbe085932df911bda5686b7bf671f53 (diff)
downloadgitbook-c681cd286a746d5cf3f69fc800bcb79a42e70973.zip
gitbook-c681cd286a746d5cf3f69fc800bcb79a42e70973.tar.gz
gitbook-c681cd286a746d5cf3f69fc800bcb79a42e70973.tar.bz2
Remove lodash dependency
Diffstat (limited to 'lib/utils/logger.js')
-rw-r--r--lib/utils/logger.js43
1 files changed, 25 insertions, 18 deletions
diff --git a/lib/utils/logger.js b/lib/utils/logger.js
index fc9c394..3bde904 100644
--- a/lib/utils/logger.js
+++ b/lib/utils/logger.js
@@ -1,21 +1,22 @@
-var _ = require('lodash');
+var is = require('is');
var util = require('util');
var color = require('bash-color');
+var Immutable = require('immutable');
-var LEVELS = {
+var LEVELS = Immutable.Map({
DEBUG: 0,
INFO: 1,
WARN: 2,
ERROR: 3,
DISABLED: 10
-};
+});
-var COLORS = {
+var COLORS = Immutable.Map({
DEBUG: color.purple,
INFO: color.cyan,
WARN: color.yellow,
ERROR: color.red
-};
+});
function Logger(write, logLevel) {
if (!(this instanceof Logger)) return new Logger(write, logLevel);
@@ -29,17 +30,18 @@ function Logger(write, logLevel) {
this.setLevel(logLevel || 'info');
- _.bindAll(this);
-
// Create easy-to-use method like "logger.debug.ln('....')"
- _.each(_.omit(LEVELS, 'DISABLED'), function(level, levelKey) {
+ LEVELS.forEach(function(level, levelKey) {
+ if (levelKey === 'DISABLED') {
+ return;
+ }
levelKey = levelKey.toLowerCase();
- this[levelKey] = _.partial(this.log, level);
- this[levelKey].ln = _.partial(this.logLn, level);
- this[levelKey].ok = _.partial(this.ok, level);
- this[levelKey].fail = _.partial(this.fail, level);
- this[levelKey].promise = _.partial(this.promise, level);
+ this[levelKey] = this.log.bind(this, level);
+ this[levelKey].ln = this.logLn.bind(this, level);
+ this[levelKey].ok = this.ok.bind(this, level);
+ this[levelKey].fail = this.fail.bind(this, level);
+ this[levelKey].promise = this.promise.bind(this, level);
}, this);
}
@@ -49,7 +51,11 @@ function Logger(write, logLevel) {
@param {String} logLevel
*/
Logger.prototype.setLevel = function(logLevel) {
- if (_.isString(logLevel)) logLevel = LEVELS[logLevel.toUpperCase()];
+ if (is.string(logLevel)) {
+ logLevel = logLevel.toUpperCase();
+ logLevel = LEVELS.get(logLevel);
+ }
+
this.logLevel = logLevel;
};
@@ -60,7 +66,7 @@ Logger.prototype.setLevel = function(logLevel) {
*/
Logger.prototype.write = function(msg) {
msg = msg.toString();
- this.lastChar = _.last(msg);
+ this.lastChar = msg[msg.length - 1];
return this._write(msg);
};
@@ -88,12 +94,14 @@ Logger.prototype.writeLn = function(msg) {
Logger.prototype.log = function(level) {
if (level < this.logLevel) return;
- var levelKey = _.findKey(LEVELS, function(v) { return v == level; });
+ var levelKey = LEVELS.findKey(function(v) {
+ return v === level;
+ });
var args = Array.prototype.slice.apply(arguments, [1]);
var msg = this.format.apply(this, args);
if (this.lastChar == '\n') {
- msg = COLORS[levelKey](levelKey.toLowerCase()+':')+' '+msg;
+ msg = COLORS.get(levelKey)(levelKey.toLowerCase()+':')+' '+msg;
}
return this.write(msg);
@@ -151,6 +159,5 @@ Logger.prototype.promise = function(level, p) {
};
Logger.LEVELS = LEVELS;
-Logger.COLORS = COLORS;
module.exports = Logger;