getBufferedLogger tested.

This commit is contained in:
Stefano Graziato 2014-08-16 14:22:58 +02:00
parent c9e72d0f00
commit 59a6703549
2 changed files with 38 additions and 34 deletions

View File

@ -74,20 +74,20 @@ function getBufferedLogger(categoryName) {
var base_logger = getLogger(categoryName); var base_logger = getLogger(categoryName);
var logger = {}; var logger = {};
logger.temp = []; logger.temp = [];
logger.base_logger = base_logger; logger.target = base_logger;
logger.flush = function () { logger.flush = function () {
for (var i = 0; i < logger.temp.length; i++) { for (var i = 0; i < logger.temp.length; i++) {
var log = logger.temp[i]; var log = logger.temp[i];
logger.base_logger[log.level](log.message); logger.target[log.level](log.message);
delete logger.temp[i]; delete logger.temp[i];
} }
}; };
logger.trace = function (message) { logger.temp.push({level: 'trace', 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.debug = function (message) { logger.temp.push({level: 'debug', message: message}); };
logger.info = function (message) { logger.temp.push({level: 'info', 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.warn = function (message) { logger.temp.push({level: 'warn', message: message}); };
logger.error = function (message) { logger.temp.push({level: 'error', 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.fatal = function (message) { logger.temp.push({level: 'fatal', message: message}); };
return logger; return logger;
} }
@ -398,6 +398,7 @@ function shutdown(cb) {
} }
module.exports = { module.exports = {
getBufferedLogger: getBufferedLogger,
getLogger: getLogger, getLogger: getLogger,
getDefaultLogger: getDefaultLogger, getDefaultLogger: getDefaultLogger,
hasLogger: hasLogger, hasLogger: hasLogger,

View File

@ -38,13 +38,12 @@ vows.describe('log4js').addBatch({
var log4js = require('../lib/log4js'); var log4js = require('../lib/log4js');
log4js.clearAppenders(); log4js.clearAppenders();
var logger = log4js.getBufferedLogger('tests'); var logger = log4js.getBufferedLogger('tests');
logger.setLevel("DEBUG");
return logger; return logger;
}, },
'should take a category and return a logger': function (logger) { 'should take a category and return a logger': function (logger) {
assert.equal(logger.category, 'tests'); assert.equal(logger.target.category, 'tests');
assert.equal(logger.level.toString(), "DEBUG"); assert.isFunction(logger.flush);
assert.isFunction(logger.trace); assert.isFunction(logger.trace);
assert.isFunction(logger.debug); assert.isFunction(logger.debug);
assert.isFunction(logger.info); assert.isFunction(logger.info);
@ -54,9 +53,12 @@ vows.describe('log4js').addBatch({
}, },
'cache events': { 'cache events': {
topic: function (logger) { topic: function () {
var log4js = require('../lib/log4js');
log4js.clearAppenders();
var logger = log4js.getBufferedLogger('tests1');
var events = []; var events = [];
logger.addListener("log", function (logEvent) { events.push(logEvent); }); logger.target.addListener("log", function (logEvent) { events.push(logEvent); });
logger.debug("Debug event"); logger.debug("Debug event");
logger.trace("Trace event 1"); logger.trace("Trace event 1");
logger.trace("Trace event 2"); logger.trace("Trace event 2");
@ -66,30 +68,31 @@ vows.describe('log4js').addBatch({
return events; 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); assert.equal(events.length, 0);
},
'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!');
} }
},
'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;
},
'should emit log events when .flush() is called.': function (events) {
assert.equal(events.length, 6);
} }
} }
}, },