From 59a670354959d3e1805fa2c7b39d59205b349364 Mon Sep 17 00:00:00 2001 From: Stefano Graziato Date: Sat, 16 Aug 2014 14:22:58 +0200 Subject: [PATCH] getBufferedLogger tested. --- lib/log4js.js | 17 +++++++------- test/logging-test.js | 55 +++++++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/lib/log4js.js b/lib/log4js.js index 86c5247..2e1f25c 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -74,20 +74,20 @@ function getBufferedLogger(categoryName) { var base_logger = getLogger(categoryName); var logger = {}; logger.temp = []; - logger.base_logger = base_logger; + logger.target = base_logger; logger.flush = function () { for (var i = 0; i < logger.temp.length; i++) { var log = logger.temp[i]; - logger.base_logger[log.level](log.message); + logger.target[log.level](log.message); delete logger.temp[i]; } }; - logger.trace = function (message) { logger.temp.push({level: 'trace', message: message}); } - logger.debug = function (message) { logger.temp.push({level: 'debug', message: message}); } - logger.info = function (message) { logger.temp.push({level: 'info', message: message}); } - logger.warn = function (message) { logger.temp.push({level: 'warn', message: message}); } - logger.error = function (message) { logger.temp.push({level: 'error', message: message}); } - logger.fatal = function (message) { logger.temp.push({level: 'fatal', message: message}); } + logger.trace = function (message) { logger.temp.push({level: 'trace', message: message}); }; + logger.debug = function (message) { logger.temp.push({level: 'debug', message: message}); }; + logger.info = function (message) { logger.temp.push({level: 'info', message: message}); }; + logger.warn = function (message) { logger.temp.push({level: 'warn', message: message}); }; + logger.error = function (message) { logger.temp.push({level: 'error', message: message}); }; + logger.fatal = function (message) { logger.temp.push({level: 'fatal', message: message}); }; return logger; } @@ -398,6 +398,7 @@ function shutdown(cb) { } module.exports = { + getBufferedLogger: getBufferedLogger, getLogger: getLogger, getDefaultLogger: getDefaultLogger, hasLogger: hasLogger, diff --git a/test/logging-test.js b/test/logging-test.js index 0b13766..a8f8894 100644 --- a/test/logging-test.js +++ b/test/logging-test.js @@ -38,13 +38,12 @@ vows.describe('log4js').addBatch({ var log4js = require('../lib/log4js'); log4js.clearAppenders(); var logger = log4js.getBufferedLogger('tests'); - logger.setLevel("DEBUG"); return logger; }, 'should take a category and return a logger': function (logger) { - assert.equal(logger.category, 'tests'); - assert.equal(logger.level.toString(), "DEBUG"); + assert.equal(logger.target.category, 'tests'); + assert.isFunction(logger.flush); assert.isFunction(logger.trace); assert.isFunction(logger.debug); assert.isFunction(logger.info); @@ -54,9 +53,12 @@ vows.describe('log4js').addBatch({ }, 'cache events': { - topic: function (logger) { + topic: function () { + var log4js = require('../lib/log4js'); + log4js.clearAppenders(); + var logger = log4js.getBufferedLogger('tests1'); var events = []; - logger.addListener("log", function (logEvent) { events.push(logEvent); }); + logger.target.addListener("log", function (logEvent) { events.push(logEvent); }); logger.debug("Debug event"); logger.trace("Trace event 1"); logger.trace("Trace event 2"); @@ -66,30 +68,31 @@ vows.describe('log4js').addBatch({ return events; }, - 'should not emit log events until .flush() is called.': function (events) { + 'should not emit log events if .flush() is not called.': function (events) { assert.equal(events.length, 0); + } + }, + + 'log events after flush() is called': { + topic: function () { + var log4js = require('../lib/log4js'); + log4js.clearAppenders(); + var logger = log4js.getBufferedLogger('tests2'); + logger.target.setLevel("TRACE"); + var events = []; + logger.target.addListener("log", function (logEvent) { events.push(logEvent); }); + logger.debug("Debug event"); + logger.trace("Trace event 1"); + logger.trace("Trace event 2"); + logger.warn("Warning event"); + logger.error("Aargh!", new Error("Pants are on fire!")); + logger.error("Simulated CouchDB problem", { err: 127, cause: "incendiary underwear" }); + logger.flush(); + return events; }, - 'log events': { - topic: function (logger) { - logger.flush(); - }, - - 'should emit log events after .flush() is called.': function (events) { - assert.equal(events[0].level.toString(), 'DEBUG'); - assert.equal(events[0].data[0], 'Debug event'); - assert.instanceOf(events[0].startTime, Date); - }, - - 'should not emit events of a lower level': function (events) { - assert.equal(events.length, 4); - assert.equal(events[1].level.toString(), 'WARN'); - }, - - 'should include the error if passed in': function (events) { - assert.instanceOf(events[2].data[1], Error); - assert.equal(events[2].data[1].message, 'Pants are on fire!'); - } + 'should emit log events when .flush() is called.': function (events) { + assert.equal(events.length, 6); } } },