Added logLevelFilter
This commit is contained in:
parent
30121affd9
commit
15e9a30d05
@ -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
28
spec/fixtures/with-logLevelFilter.json
vendored
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
@ -1,19 +1,18 @@
|
|||||||
posix = require('posix');
|
posix = require('posix');
|
||||||
|
|
||||||
waitForWriteAndThenRead = function(filename) {
|
waitForWriteAndThenRead = function (filename) {
|
||||||
//here's the tricky part - writes are asynchronous
|
//here's the tricky part - writes are asynchronous
|
||||||
//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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user