From 5bd7ce3ab997b32bc9dce3da1958ad952ea84d1b Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 2 Aug 2013 15:12:04 +1000 Subject: [PATCH] working, except for tests which expect log levels to persist across getLogger calls --- lib/log4js.js | 14 +++++++++----- lib/logger.js | 13 ++++++++----- test/logger-test.js | 15 ++++++--------- test/logging-test.js | 1 + 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/lib/log4js.js b/lib/log4js.js index d23aefa..9f4370c 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -43,7 +43,7 @@ * @static * Website: http://log4js.berlios.de */ -var debug = require('./debug')('log4js-core') +var debug = require('./debug')('core') , fs = require('fs') , path = require('path') , util = require('util') @@ -77,7 +77,7 @@ function getLogger (categoryName) { debug("getLogger(" + categoryName + ")"); - return new Logger(categoryName, levels.toLevel(categoryLevels[categoryName]) || null); + return new Logger(categoryName, categoryLevels[categoryName] || null, dispatch); } /** @@ -151,18 +151,22 @@ function getDefaultLogger () { * This would be a good place to implement category hierarchies/wildcards, etc */ function dispatch(event) { - debug("event is " + util.inspect(event)); - if (appenders[event.category]) { - dispatchToCategory(event.category, event); + debug("event is " + util.inspect(event)); + debug("appenders is " + util.inspect(appenders)); + if (appenders[event.categoryName]) { + debug("appender defined for " + event.categoryName); + dispatchToCategory(event.categoryName, event); } if (appenders[ALL_CATEGORIES]) { + debug("appender defined for " + ALL_CATEGORIES); dispatchToCategory(ALL_CATEGORIES, event); } } function dispatchToCategory(category, event) { appenders[category].forEach(function(appender) { + debug("Sending " + util.inspect(event) + " to appender " + appender); appender(event); }); } diff --git a/lib/logger.js b/lib/logger.js index 03d6f04..5a1f81b 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -1,7 +1,7 @@ "use strict"; -var levels = require('./levels') +var debug = require('./debug')('logger') +, levels = require('./levels') , util = require('util') -, log4js = require('./log4js') , DEFAULT_CATEGORY = '[default]'; /** @@ -26,17 +26,20 @@ function LoggingEvent (categoryName, level, data) { * @param name name of category to log to * @author Stephan Strittmatter */ -function Logger (name, level) { +function Logger (name, level, dispatch) { this.category = name || DEFAULT_CATEGORY; if (level) { this.setLevel(level); } + + this.dispatch = dispatch; } Logger.DEFAULT_CATEGORY = DEFAULT_CATEGORY; Logger.prototype.level = levels.TRACE; Logger.prototype.setLevel = function(level) { + debug("setting level to " + level); this.level = levels.toLevel(level, this.level || levels.TRACE); }; @@ -48,8 +51,8 @@ Logger.prototype.log = function() { var args = Array.prototype.slice.call(arguments) , logLevel = args.shift() , loggingEvent = new LoggingEvent(this.category, logLevel, args); - - log4js.dispatch(loggingEvent); + debug("Logging event " + loggingEvent + " to dispatch = " + util.inspect(this.dispatch)); + this.dispatch(loggingEvent); }; Logger.prototype.isLevelEnabled = function(otherLevel) { diff --git a/test/logger-test.js b/test/logger-test.js index 9ff31df..e47e8e4 100644 --- a/test/logger-test.js +++ b/test/logger-test.js @@ -57,16 +57,13 @@ vows.describe('../lib/logger').addBatch({ }, 'log': { - topic: new Logger('testing'), - 'should send log events to log4js': function(logger) { - var evt, original = log4js.dispatch; - log4js.dispatch = function(event) { - evt = event; - }; - + topic: function() { + var evt + , logger = new Logger('testing', null, function(event) { evt = event; }); logger.log(levels.DEBUG, "cheese"); - log4js.dispatch = original; - + return evt; + }, + 'should send log events to log4js': function(evt) { assert.equal(evt.categoryName, 'testing'); assert.equal(evt.level, levels.DEBUG); assert.equal(evt.data[0], "cheese"); diff --git a/test/logging-test.js b/test/logging-test.js index f173e41..8615ac0 100644 --- a/test/logging-test.js +++ b/test/logging-test.js @@ -54,6 +54,7 @@ vows.describe('log4js').addBatch({ 'log events' : { topic: function(logger) { var events = []; + var log4js = require('../lib/log4js'); log4js.addAppender(function (logEvent) { events.push(logEvent); }, "tests"); logger.debug("Debug event"); logger.trace("Trace event 1");