Skip to content

Commit

Permalink
improve logger to call proper console api
Browse files Browse the repository at this point in the history
solved es-analysis#185 (redirecting stderr in shell)
  • Loading branch information
kyungilpark committed Jun 28, 2019
1 parent 2c2e09c commit 2a06276
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ var levels = [
'WARNING',
'ERROR'
];
var apiMap = {
'TRACE': 'trace',
'DEBUG': 'log',
'INFO': 'info',
'WARNING': 'warn',
'ERROR': 'error'
};

levels.forEach(function(level, i){
Logger[level] = i;
Logger.prototype[level.toLowerCase()] = function() {
if (i >= this.level) console.log.apply(console,arguments);
if (i >= this.level) console[apiMap[level]].apply(console,arguments);
};
});
133 changes: 133 additions & 0 deletions test/logger_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
'use strict';
var Logger = require('../lib/logger');

exports['logger'] = {
setUp: function(done) {
// setup here
this._console = console;
console = {
msg: {},
trace: function(msg) {
this.msg.trace = msg;
},
log: function(msg) {
this.msg.log = msg;
},
info: function(msg) {
this.msg.info = msg;
},
warn: function(msg) {
this.msg.warn = msg;
},
error: function(msg) {
this.msg.error = msg;
}
};
done();
},
tearDown: function (done) {
// clean up
console = this._console;
done();
},
'TRACE LEVEL': function(test) {
var message = "called";
var log = new Logger(Logger.TRACE);

test.expect(5);

log.trace(message);
log.debug(message);
log.info(message);
log.warning(message);
log.error(message);

test.equal(console.msg.trace, message, 'should trace message');
test.equal(console.msg.log, message, 'should log message');
test.equal(console.msg.info, message, 'should info message');
test.equal(console.msg.warn, message, 'should warn message');
test.equal(console.msg.error, message, 'should error message');

test.done();
},
'DEBUG LEVEL': function(test) {
var message = "called";
var log = new Logger(Logger.DEBUG);

test.expect(5);

log.trace(message);
log.debug(message);
log.info(message);
log.warning(message);
log.error(message);

test.equal(console.msg.trace, undefined, 'should not trace message');
test.equal(console.msg.log, message, 'should log message');
test.equal(console.msg.info, message, 'should info message');
test.equal(console.msg.warn, message, 'should warn message');
test.equal(console.msg.error, message, 'should error message');

test.done();
},
'INFO LEVEL': function(test) {
var message = "called";
var log = new Logger(Logger.INFO);

test.expect(5);

log.trace(message);
log.debug(message);
log.info(message);
log.warning(message);
log.error(message);

test.equal(console.msg.trace, undefined, 'should not trace message');
test.equal(console.msg.log, undefined, 'should not log message');
test.equal(console.msg.info, message, 'should info message');
test.equal(console.msg.warn, message, 'should warn message');
test.equal(console.msg.error, message, 'should error message');

test.done();
},
'WARNING LEVEL': function(test) {
var message = "called";
var log = new Logger(Logger.WARNING);

test.expect(5);

log.trace(message);
log.debug(message);
log.info(message);
log.warning(message);
log.error(message);

test.equal(console.msg.trace, undefined, 'should not trace message');
test.equal(console.msg.log, undefined, 'should not log message');
test.equal(console.msg.info, undefined, 'should not info message');
test.equal(console.msg.warn, message, 'should warn message');
test.equal(console.msg.error, message, 'should error message');

test.done();
},
'ERROR LEVEL': function(test) {
var message = "called";
var log = new Logger(Logger.ERROR);

test.expect(5);

log.trace(message);
log.debug(message);
log.info(message);
log.warning(message);
log.error(message);

test.equal(console.msg.trace, undefined, 'should not trace message');
test.equal(console.msg.log, undefined, 'should not log message');
test.equal(console.msg.info, undefined, 'should not info message');
test.equal(console.msg.warn, undefined, 'should not warn message');
test.equal(console.msg.error, message, 'should error message');

test.done();
}
};

0 comments on commit 2a06276

Please sign in to comment.