From c9e72d0f0024ce6133de18bbf9f8c238769b37ce Mon Sep 17 00:00:00 2001 From: Stefano Graziato Date: Fri, 15 Aug 2014 13:42:08 +0200 Subject: [PATCH] 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. --- lib/log4js.js | 23 ++++++++++++++++ test/logging-test.js | 63 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/lib/log4js.js b/lib/log4js.js index 35a383d..86c5247 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -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. diff --git a/test/logging-test.js b/test/logging-test.js index a62de31..0b13766 100644 --- a/test/logging-test.js +++ b/test/logging-test.js @@ -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');