Added logLevelFilter

This commit is contained in:
csausdev 2010-01-19 22:26:54 +11:00
parent 30121affd9
commit 15e9a30d05
3 changed files with 104 additions and 21 deletions

View File

@ -184,6 +184,10 @@ var appenderMakers = {
layout = layoutMakers[config.layout.type](config.layout); layout = layoutMakers[config.layout.type](config.layout);
} }
return consoleAppender(layout); return consoleAppender(layout);
},
"logLevelFilter": function(config) {
var appender = appenderMakers[config.appender.type](config.appender);
return logLevelFilter(config.level, appender);
} }
}; };
@ -196,15 +200,20 @@ var layoutMakers = {
} }
}; };
function configureAppender(appenderConfig) {
}
function configureAppenders(appenderList) { function configureAppenders(appenderList) {
log4js.clearAppenders(); log4js.clearAppenders();
if (appenderList) { if (appenderList) {
appenderList.forEach( appenderList.forEach(function(appenderConfig) {
function(appenderConfig) {
var appender = appenderMakers[appenderConfig.type](appenderConfig); var appender = appenderMakers[appenderConfig.type](appenderConfig);
if (appender) {
log4js.addAppender(appender, appenderConfig.category); log4js.addAppender(appender, appenderConfig.category);
} else {
throw new Error("log4js configuration problem for "+sys.inspect(appenderConfig));
} }
); });
} else { } else {
log4js.addAppender(consoleAppender); log4js.addAppender(consoleAppender);
} }
@ -263,6 +272,10 @@ Level.prototype.isLessThanOrEqualTo = function(otherLevel) {
return this.level <= otherLevel.level; return this.level <= otherLevel.level;
}; };
Level.prototype.isGreaterThanOrEqualTo = function(otherLevel) {
return this.level >= otherLevel.level;
};
/** /**
* Models a logging event. * Models a logging event.
* @constructor * @constructor
@ -369,15 +382,9 @@ consoleAppender = function (layout) {
fileAppender = function(file, layout) { fileAppender = function(file, layout) {
layout = layout || basicLayout; layout = layout || basicLayout;
file = file || "log4js.log"; file = file || "log4js.log";
var logFile;
//waits are generally bad, but we need //waits are generally bad, but we need
//the file to be open before we start doing any writing. //the file to be open before we start doing any writing.
posix var logFile = posix.open(file, process.O_APPEND | process.O_WRONLY | process.O_CREAT, 0644).wait();
.open(file, process.O_APPEND | process.O_WRONLY | process.O_CREAT, 0644)
.addCallback(function(fileDescriptor) { logFile = fileDescriptor; })
.addErrback(function(err) { throw new Error("Could not open logfile "+file+", error was: "+sys.inspect(err)); })
.wait();
//register ourselves as listeners for shutdown //register ourselves as listeners for shutdown
//so that we can close the file. //so that we can close the file.
//not entirely sure this is necessary, but still. //not entirely sure this is necessary, but still.
@ -388,6 +395,15 @@ fileAppender = function(file, layout) {
}; };
}; };
logLevelFilter = function(levelString, appender) {
var level = Level.toLevel(levelString);
return function(logEvent) {
if (logEvent.level.isGreaterThanOrEqualTo(level)) {
appender(logEvent);
}
}
};
/** /**
* BasicLayout is a simple layout for storing the logs. The logs are stored * BasicLayout is a simple layout for storing the logs. The logs are stored
* in following format: * in following format:
@ -596,6 +612,7 @@ Date.prototype.toFormattedString = function(format) {
log4js.consoleAppender = consoleAppender; log4js.consoleAppender = consoleAppender;
log4js.fileAppender = fileAppender; log4js.fileAppender = fileAppender;
log4js.logLevelFilter = logLevelFilter;
log4js.basicLayout = basicLayout; log4js.basicLayout = basicLayout;
log4js.patternLayout = patternLayout; log4js.patternLayout = patternLayout;
log4js.messagePassThroughLayout = messagePassThroughLayout; log4js.messagePassThroughLayout = messagePassThroughLayout;

28
spec/fixtures/with-logLevelFilter.json vendored Normal file
View File

@ -0,0 +1,28 @@
{
appenders: [
{
category: "tests",
type: "logLevelFilter",
level: "WARN",
appender: {
type: "file",
filename: "tmp-tests-warnings.log",
layout: {
type: "messagePassThrough"
}
}
},
{
category: "tests",
type: "file",
filename: "tmp-tests.log",
layout: {
type: "messagePassThrough"
}
}
],
levels: {
"tests": "DEBUG"
}
}

View File

@ -5,15 +5,14 @@ waitForWriteAndThenRead = function(filename) {
//so I'm going to make a promise, wait a bit and then //so I'm going to make a promise, wait a bit and then
//try to read the file. //try to read the file.
var content, promise = new process.Promise(); var content, promise = new process.Promise();
promise.addCallback(function() {
content = posix.cat(filename).timeout(500).wait();
});
setTimeout(function() { setTimeout(function() {
promise.emitSuccess(); promise.emitSuccess();
}, 50); }, 0);
promise.addCallback(function() {
posix.cat(filename).addCallback(
function(fileContents) { content = fileContents; }
).wait();
}).wait();
promise.wait();
return content; return content;
} }
@ -161,14 +160,40 @@ describe 'log4js'
end end
end end
describe 'logLevelFilter'
it 'should only pass log events greater than or equal to its own level'
var logEvent;
log4js.addAppender(log4js.logLevelFilter('ERROR', function(evt) { logEvent = evt; }));
logger.debug('this should not trigger an event');
logEvent.should.be undefined
logger.warn('neither should this');
logEvent.should.be undefined
logger.error('this should, though');
logEvent.should.not.be undefined
logEvent.message.should.be 'this should, though'
logger.fatal('so should this')
logEvent.message.should.be 'so should this'
end
end
describe 'configure' describe 'configure'
before before_each
log4js.clearAppenders(); log4js.clearAppenders();
try { try {
posix.unlink('./tmp-tests.log').wait(); posix.unlink('./tmp-tests.log').wait();
} catch(e) { } catch(e) {
print('Could not delete tmp-tests.log: '+e.message); print('Could not delete tmp-tests.log: '+e.message);
} }
try {
posix.unlink('./tmp-tests-warnings.log').wait();
} catch (e) {
print('Could not delete tmp-tests-warnings.log: '+e.message);
}
end end
it 'should load appender configuration from a json file' it 'should load appender configuration from a json file'
@ -184,6 +209,19 @@ describe 'log4js'
event.message.should.be 'this should fire an event' event.message.should.be 'this should fire an event'
waitForWriteAndThenRead('./tmp-tests.log').should.be 'this should fire an event\n' waitForWriteAndThenRead('./tmp-tests.log').should.be 'this should fire an event\n'
end end
it 'should handle logLevelFilter configuration'
log4js.configure('spec/fixtures/with-logLevelFilter.json');
event = undefined;
logger.info('main');
logger.error('both');
logger.warn('both');
logger.debug('main');
waitForWriteAndThenRead('./tmp-tests.log').should.be 'main\nboth\nboth\nmain\n'
waitForWriteAndThenRead('./tmp-tests-warnings.log').should.be 'both\nboth\n'
end
end end
end end