added getBufferedLogger function.
This function should be useful when you need to log during async parallel operations, without having a mess in logs. For example when you walk asynchronously a directory and you want logs to be grouped by file. It returns the same getLogger() object but messages are stored internally and sent to appenders only when you call the flush() method on it.
This commit is contained in:
parent
a27345461b
commit
c9e72d0f00
@ -70,6 +70,29 @@ function hasLogger(logger) {
|
||||
}
|
||||
|
||||
|
||||
function getBufferedLogger(categoryName) {
|
||||
var base_logger = getLogger(categoryName);
|
||||
var logger = {};
|
||||
logger.temp = [];
|
||||
logger.base_logger = 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);
|
||||
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}); }
|
||||
|
||||
return logger;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a logger instance. Instance is cached on categoryName level.
|
||||
* @param {String} categoryName name of category to log to.
|
||||
|
@ -32,6 +32,69 @@ function setupConsoleTest() {
|
||||
}
|
||||
|
||||
vows.describe('log4js').addBatch({
|
||||
|
||||
'getBufferedLogger': {
|
||||
topic: function () {
|
||||
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.isFunction(logger.trace);
|
||||
assert.isFunction(logger.debug);
|
||||
assert.isFunction(logger.info);
|
||||
assert.isFunction(logger.warn);
|
||||
assert.isFunction(logger.error);
|
||||
assert.isFunction(logger.fatal);
|
||||
},
|
||||
|
||||
'cache events': {
|
||||
topic: function (logger) {
|
||||
var events = [];
|
||||
logger.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" });
|
||||
return events;
|
||||
},
|
||||
|
||||
'should not emit log events until .flush() is called.': function (events) {
|
||||
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!');
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
'getLogger': {
|
||||
topic: function() {
|
||||
var log4js = require('../lib/log4js');
|
||||
|
Loading…
Reference in New Issue
Block a user