appenders can be added to multiple categories at the same time

This commit is contained in:
Gareth Jones 2010-10-07 12:15:23 +08:00
parent caa7338b04
commit a7006444b3
4 changed files with 70 additions and 8 deletions

View File

@ -55,7 +55,7 @@ var log4js = {
* @static * @static
* @final * @final
*/ */
version: "0.1.0", version: "0.1.1",
/** /**
* Date of logger initialized. * Date of logger initialized.
@ -119,8 +119,21 @@ exports.getDefaultLogger = log4js.getDefaultLogger = function() {
return log4js.getLogger(DEFAULT_CATEGORY); return log4js.getLogger(DEFAULT_CATEGORY);
}; };
exports.addAppender = log4js.addAppender = function (appender, categoryName) { /**
var category = categoryName || ALL_CATEGORIES; * args are appender, then zero or more categories
*/
exports.addAppender = log4js.addAppender = function () {
var args = Array.prototype.slice.call(arguments);
var appender = args.shift();
if (args.length == 0) {
args = [ ALL_CATEGORIES ];
}
//argument may already be an array
if (args[0].forEach) {
args = args[0];
}
args.forEach(function(category) {
if (!log4js.appenders[category]) { if (!log4js.appenders[category]) {
log4js.appenders[category] = []; log4js.appenders[category] = [];
} }
@ -135,13 +148,14 @@ exports.addAppender = log4js.addAppender = function (appender, categoryName) {
} else if (log4js.loggers[category]) { } else if (log4js.loggers[category]) {
log4js.loggers[category].addListener("log", appender); log4js.loggers[category].addListener("log", appender);
} }
});
}; };
exports.clearAppenders = log4js.clearAppenders = function() { exports.clearAppenders = log4js.clearAppenders = function() {
log4js.appenders = []; log4js.appenders = [];
for (var logger in log4js.loggers) { for (var logger in log4js.loggers) {
if (log4js.loggers.hasOwnProperty(logger)) { if (log4js.loggers.hasOwnProperty(logger)) {
log4js.loggers[logger].listeners.length = 0; log4js.loggers[logger].removeAllListeners("log");
} }
} }
}; };

View File

@ -1,6 +1,6 @@
{ {
"name": "log4js", "name": "log4js",
"version": "0.1.0", "version": "0.1.1",
"description": "Port of Log4js to work with node.", "description": "Port of Log4js to work with node.",
"keywords": [ "keywords": [
"logging", "logging",

View File

@ -1770,4 +1770,4 @@
} }
}) })
})() })()

View File

@ -48,7 +48,6 @@ describe 'log4js'
describe 'addAppender' describe 'addAppender'
before_each before_each
log4js.clearAppenders();
appenderEvent = undefined; appenderEvent = undefined;
appender = function(logEvent) { appenderEvent = logEvent; }; appender = function(logEvent) { appenderEvent = logEvent; };
end end
@ -72,6 +71,15 @@ describe 'log4js'
appenderEvent.should.be event appenderEvent.should.be event
otherEvent.should.be event otherEvent.should.be event
otherEvent = undefined;
appenderEvent = undefined;
log4js.getLogger('pants').debug("this should not be propagated to otherEvent");
otherEvent.should.be undefined
appenderEvent.should.not.be undefined
appenderEvent.message.should.be "this should not be propagated to otherEvent"
cheeseLogger = null;
end end
end end
@ -88,6 +96,45 @@ describe 'log4js'
end end
end end
describe 'with multiple categories'
it 'should register the function as a listener for all the categories'
log4js.addAppender(appender, 'tests', 'biscuits');
logger.debug('this is a test');
appenderEvent.should.be event
appenderEvent = undefined;
var otherLogger = log4js.getLogger('biscuits');
otherLogger.debug("mmm... garibaldis");
appenderEvent.should.not.be undefined
appenderEvent.message.should.be "mmm... garibaldis"
appenderEvent = undefined;
otherLogger = null;
log4js.getLogger("something else").debug("pants");
appenderEvent.should.be undefined
end
it 'should register the function when the list of categories is an array'
log4js.addAppender(appender, ['tests', 'pants']);
logger.debug('this is a test');
appenderEvent.should.be event
appenderEvent = undefined;
var otherLogger = log4js.getLogger('pants');
otherLogger.debug("big pants");
appenderEvent.should.not.be undefined
appenderEvent.message.should.be "big pants"
appenderEvent = undefined;
otherLogger = null;
log4js.getLogger("something else").debug("pants");
appenderEvent.should.be undefined
end
end
end end
describe 'basicLayout' describe 'basicLayout'
@ -189,6 +236,8 @@ describe 'log4js'
//and sets the log level for "tests" to WARN //and sets the log level for "tests" to WARN
log4js.configure('spec/fixtures/log4js.json'); log4js.configure('spec/fixtures/log4js.json');
event = undefined; event = undefined;
logger = log4js.getLogger("tests");
logger.addListener("log", function(evt) { event = evt });
logger.info('this should not fire an event'); logger.info('this should not fire an event');
event.should.be undefined event.should.be undefined
@ -200,7 +249,6 @@ describe 'log4js'
it 'should handle logLevelFilter configuration' it 'should handle logLevelFilter configuration'
log4js.configure('spec/fixtures/with-logLevelFilter.json'); log4js.configure('spec/fixtures/with-logLevelFilter.json');
event = undefined;
logger.info('main'); logger.info('main');
logger.error('both'); logger.error('both');