refactoring to allow dependency injection

This commit is contained in:
csausdev
2010-12-04 17:49:39 +08:00
committed by Gareth Jones
parent fc3d50846d
commit 76fea28bbb
6 changed files with 481 additions and 673 deletions

View File

@@ -1,16 +0,0 @@
{
"appenders": [
{
"category": "tests",
"type": "file",
"filename": "tmp-tests.log",
"layout": {
"type": "messagePassThrough"
}
}
],
"levels": {
"tests": "WARN"
}
}

View File

@@ -1,28 +0,0 @@
{
"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

@@ -1,12 +1,7 @@
describe 'log4js'
before
extend(context, {
log4js : require("log4js"),
fs: require("fs"),
waitForWriteAndThenReadFile : function (filename) {
process.loop();
return fs.readFileSync(filename, "utf8");
}
log4js : require("log4js")()
});
end
@@ -17,35 +12,7 @@ describe 'log4js'
logger.setLevel("TRACE");
logger.addListener("log", function (logEvent) { event = logEvent; });
end
describe 'getLogger'
it 'should take a category and return a Logger'
logger.category.should.be 'tests'
logger.level.should.be log4js.levels.TRACE
logger.should.respond_to 'debug'
logger.should.respond_to 'info'
logger.should.respond_to 'warn'
logger.should.respond_to 'error'
logger.should.respond_to 'fatal'
end
it 'should emit log events'
logger.trace("Trace event");
event.level.toString().should.be 'TRACE'
event.message.should.be 'Trace event'
event.startTime.should.not.be undefined
end
it 'should not emit events of a lower level than the minimum'
logger.setLevel("DEBUG");
event = undefined;
logger.trace("This should not generate a log message");
event.should.be undefined
end
end
describe 'addAppender'
before_each
appenderEvent = undefined;
@@ -177,24 +144,6 @@ describe 'log4js'
end
end
describe 'fileAppender'
before
log4js.clearAppenders();
try {
fs.unlinkSync('./tmp-tests.log');
} catch(e) {
//print('Could not delete tmp-tests.log: '+e.message);
}
end
it 'should write log events to a file'
log4js.addAppender(log4js.fileAppender('./tmp-tests.log', log4js.messagePassThroughLayout), 'tests');
logger.debug('this is a test');
waitForWriteAndThenReadFile('./tmp-tests.log').should.be 'this is a test\n'
end
end
describe 'logLevelFilter'
it 'should only pass log events greater than or equal to its own level'
@@ -216,49 +165,6 @@ describe 'log4js'
end
describe 'configure'
before_each
log4js.clearAppenders();
try {
fs.unlinkSync('./tmp-tests.log');
} catch(e) {
//print('Could not delete tmp-tests.log: '+e.message);
}
try {
fs.unlinkSync('./tmp-tests-warnings.log');
} catch (e) {
//print('Could not delete tmp-tests-warnings.log: '+e.message);
}
end
it 'should load appender configuration from a json file'
//this config file defines one file appender (to ./tmp-tests.log)
//and sets the log level for "tests" to WARN
log4js.configure('spec/fixtures/log4js.json');
event = undefined;
logger = log4js.getLogger("tests");
logger.addListener("log", function(evt) { event = evt });
logger.info('this should not fire an event');
event.should.be undefined
logger.warn('this should fire an event');
event.message.should.be 'this should fire an event'
waitForWriteAndThenReadFile('./tmp-tests.log').should.be 'this should fire an event\n'
end
it 'should handle logLevelFilter configuration'
log4js.configure('spec/fixtures/with-logLevelFilter.json');
logger.info('main');
logger.error('both');
logger.warn('both');
logger.debug('main');
waitForWriteAndThenReadFile('./tmp-tests.log').should.be 'main\nboth\nboth\nmain\n'
waitForWriteAndThenReadFile('./tmp-tests-warnings.log').should.be 'both\nboth\n'
end
end
end