From 2a0627671ccc887b81d3689abb075c079af0e62b Mon Sep 17 00:00:00 2001 From: Kyungil Park Date: Fri, 28 Jun 2019 23:57:26 +0900 Subject: [PATCH] improve logger to call proper console api solved #185 (redirecting stderr in shell) --- lib/logger.js | 9 ++- test/logger_test.js | 133 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 test/logger_test.js diff --git a/lib/logger.js b/lib/logger.js index b443b6ad..f33d7975 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -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); }; }); diff --git a/test/logger_test.js b/test/logger_test.js new file mode 100644 index 00000000..fc7a8700 --- /dev/null +++ b/test/logger_test.js @@ -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(); + } +};