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
* 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);
});
}

View File

@ -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) {

View File

@ -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");

View File

@ -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");