From c9e72d0f0024ce6133de18bbf9f8c238769b37ce Mon Sep 17 00:00:00 2001 From: Stefano Graziato Date: Fri, 15 Aug 2014 13:42:08 +0200 Subject: [PATCH 1/3] 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'); From 59a670354959d3e1805fa2c7b39d59205b349364 Mon Sep 17 00:00:00 2001 From: Stefano Graziato Date: Sat, 16 Aug 2014 14:22:58 +0200 Subject: [PATCH 2/3] 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); } } }, From c0aa8c5c8669a112da346e75951e3d9d30e9bcec Mon Sep 17 00:00:00 2001 From: Stefano Graziato Date: Sat, 16 Aug 2014 14:52:00 +0200 Subject: [PATCH 3/3] gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 268c88d..d213f7d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ node_modules .bob/ test/streams/test-rolling-file-stream* test/streams/test-rolling-stream-with-existing-files* +.idea