Merge pull request #236 from jchmura/filter-max-level

Added option for maximum level in logLevelFilter
This commit is contained in:
Gareth Jones 2014-08-26 09:55:13 +10:00
commit af1ce2933b
3 changed files with 39 additions and 11 deletions

View File

@ -2,10 +2,12 @@
var levels = require('../levels')
, log4js = require('../log4js');
function logLevelFilter (levelString, appender) {
var level = levels.toLevel(levelString);
function logLevelFilter (minLevelString, maxLevelString, appender) {
var minLevel = levels.toLevel(minLevelString);
var maxLevel = levels.toLevel(maxLevelString, levels.FATAL);
return function(logEvent) {
if (logEvent.level.isGreaterThanOrEqualTo(level)) {
var eventLevel = logEvent.level;
if (eventLevel.isGreaterThanOrEqualTo(minLevel) && eventLevel.isLessThanOrEqualTo(maxLevel)) {
appender(logEvent);
}
};
@ -14,7 +16,7 @@ function logLevelFilter (levelString, appender) {
function configure(config) {
log4js.loadAppender(config.appender.type);
var appender = log4js.appenderMakers[config.appender.type](config.appender);
return logLevelFilter(config.level, appender);
return logLevelFilter(config.level, config.maxLevel, appender);
}
exports.appender = logLevelFilter;

View File

@ -22,6 +22,7 @@ vows.describe('log4js logLevelFilter').addBatch({
require('../lib/appenders/logLevelFilter')
.appender(
'ERROR',
undefined,
function(evt) { logEvents.push(evt); }
),
"logLevelTest"
@ -48,13 +49,16 @@ vows.describe('log4js logLevelFilter').addBatch({
remove(__dirname + '/logLevelFilter.log');
remove(__dirname + '/logLevelFilter-warnings.log');
remove(__dirname + '/logLevelFilter-debugs.log');
log4js.configure('test/with-logLevelFilter.json');
logger = log4js.getLogger("tests");
logger.info('main');
logger.error('both');
logger.warn('both');
logger.debug('main');
logger.debug('debug');
logger.info('info');
logger.error('error');
logger.warn('warn');
logger.debug('debug');
logger.trace('trace');
//wait for the file system to catch up
setTimeout(this.callback, 500);
},
@ -64,7 +68,7 @@ vows.describe('log4js logLevelFilter').addBatch({
},
'should contain all log messages': function (contents) {
var messages = contents.trim().split(EOL);
assert.deepEqual(messages, ['main','both','both','main']);
assert.deepEqual(messages, ['debug','info','error','warn','debug','trace']);
}
},
'tmp-tests-warnings.log': {
@ -73,7 +77,16 @@ vows.describe('log4js logLevelFilter').addBatch({
},
'should contain only error and warning log messages': function(contents) {
var messages = contents.trim().split(EOL);
assert.deepEqual(messages, ['both','both']);
assert.deepEqual(messages, ['error','warn']);
}
},
'tmp-tests-debugs.log': {
topic: function() {
fs.readFile(__dirname + '/logLevelFilter-debugs.log','utf8',this.callback);
},
'should contain only trace and debug log messages': function(contents) {
var messages = contents.trim().split(EOL);
assert.deepEqual(messages, ['debug','debug','trace']);
}
}
}

View File

@ -12,6 +12,19 @@
}
}
},
{
"category": "tests",
"type": "logLevelFilter",
"level": "TRACE",
"maxLevel": "DEBUG",
"appender": {
"type": "file",
"filename": "test/logLevelFilter-debugs.log",
"layout": {
"type": "messagePassThrough"
}
}
},
{
"category": "tests",
"type": "file",
@ -23,6 +36,6 @@
],
"levels": {
"tests": "DEBUG"
"tests": "TRACE"
}
}