working, except for tests which expect log levels to persist across getLogger calls

This commit is contained in:
Gareth Jones 2013-08-02 15:12:04 +10:00
parent 3b55aefe6f
commit 5bd7ce3ab9
4 changed files with 24 additions and 19 deletions

View File

@ -43,7 +43,7 @@
* @static * @static
* Website: http://log4js.berlios.de * Website: http://log4js.berlios.de
*/ */
var debug = require('./debug')('log4js-core') var debug = require('./debug')('core')
, fs = require('fs') , fs = require('fs')
, path = require('path') , path = require('path')
, util = require('util') , util = require('util')
@ -77,7 +77,7 @@ function getLogger (categoryName) {
debug("getLogger(" + categoryName + ")"); debug("getLogger(" + categoryName + ")");
return new Logger(categoryName, levels.toLevel(categoryLevels[categoryName]) || null); return new Logger(categoryName, categoryLevels[categoryName] || null, dispatch);
} }
/** /**
@ -152,17 +152,21 @@ function getDefaultLogger () {
*/ */
function dispatch(event) { function dispatch(event) {
debug("event is " + util.inspect(event)); debug("event is " + util.inspect(event));
if (appenders[event.category]) { debug("appenders is " + util.inspect(appenders));
dispatchToCategory(event.category, event); if (appenders[event.categoryName]) {
debug("appender defined for " + event.categoryName);
dispatchToCategory(event.categoryName, event);
} }
if (appenders[ALL_CATEGORIES]) { if (appenders[ALL_CATEGORIES]) {
debug("appender defined for " + ALL_CATEGORIES);
dispatchToCategory(ALL_CATEGORIES, event); dispatchToCategory(ALL_CATEGORIES, event);
} }
} }
function dispatchToCategory(category, event) { function dispatchToCategory(category, event) {
appenders[category].forEach(function(appender) { appenders[category].forEach(function(appender) {
debug("Sending " + util.inspect(event) + " to appender " + appender);
appender(event); appender(event);
}); });
} }

View File

@ -1,7 +1,7 @@
"use strict"; "use strict";
var levels = require('./levels') var debug = require('./debug')('logger')
, levels = require('./levels')
, util = require('util') , util = require('util')
, log4js = require('./log4js')
, DEFAULT_CATEGORY = '[default]'; , DEFAULT_CATEGORY = '[default]';
/** /**
@ -26,17 +26,20 @@ function LoggingEvent (categoryName, level, data) {
* @param name name of category to log to * @param name name of category to log to
* @author Stephan Strittmatter * @author Stephan Strittmatter
*/ */
function Logger (name, level) { function Logger (name, level, dispatch) {
this.category = name || DEFAULT_CATEGORY; this.category = name || DEFAULT_CATEGORY;
if (level) { if (level) {
this.setLevel(level); this.setLevel(level);
} }
this.dispatch = dispatch;
} }
Logger.DEFAULT_CATEGORY = DEFAULT_CATEGORY; Logger.DEFAULT_CATEGORY = DEFAULT_CATEGORY;
Logger.prototype.level = levels.TRACE; Logger.prototype.level = levels.TRACE;
Logger.prototype.setLevel = function(level) { Logger.prototype.setLevel = function(level) {
debug("setting level to " + level);
this.level = levels.toLevel(level, this.level || levels.TRACE); this.level = levels.toLevel(level, this.level || levels.TRACE);
}; };
@ -48,8 +51,8 @@ Logger.prototype.log = function() {
var args = Array.prototype.slice.call(arguments) var args = Array.prototype.slice.call(arguments)
, logLevel = args.shift() , logLevel = args.shift()
, loggingEvent = new LoggingEvent(this.category, logLevel, args); , loggingEvent = new LoggingEvent(this.category, logLevel, args);
debug("Logging event " + loggingEvent + " to dispatch = " + util.inspect(this.dispatch));
log4js.dispatch(loggingEvent); this.dispatch(loggingEvent);
}; };
Logger.prototype.isLevelEnabled = function(otherLevel) { Logger.prototype.isLevelEnabled = function(otherLevel) {

View File

@ -57,16 +57,13 @@ vows.describe('../lib/logger').addBatch({
}, },
'log': { 'log': {
topic: new Logger('testing'), topic: function() {
'should send log events to log4js': function(logger) { var evt
var evt, original = log4js.dispatch; , logger = new Logger('testing', null, function(event) { evt = event; });
log4js.dispatch = function(event) {
evt = event;
};
logger.log(levels.DEBUG, "cheese"); 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.categoryName, 'testing');
assert.equal(evt.level, levels.DEBUG); assert.equal(evt.level, levels.DEBUG);
assert.equal(evt.data[0], "cheese"); assert.equal(evt.data[0], "cheese");

View File

@ -54,6 +54,7 @@ vows.describe('log4js').addBatch({
'log events' : { 'log events' : {
topic: function(logger) { topic: function(logger) {
var events = []; var events = [];
var log4js = require('../lib/log4js');
log4js.addAppender(function (logEvent) { events.push(logEvent); }, "tests"); log4js.addAppender(function (logEvent) { events.push(logEvent); }, "tests");
logger.debug("Debug event"); logger.debug("Debug event");
logger.trace("Trace event 1"); logger.trace("Trace event 1");