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

@ -4,20 +4,21 @@ This is a conversion of the [log4js](http://log4js.berlios.de/index.html)
framework to work with [node](http://nodejs.org). I've mainly stripped out the browser-specific code framework to work with [node](http://nodejs.org). I've mainly stripped out the browser-specific code
and tidied up some of the javascript. and tidied up some of the javascript.
NOTE: since v0.2.0 require('log4js') returns a function, so you need to call that function in your code before you can use it. I've done this to make testing easier (allows dependency injection).
## installation ## installation
npm install log4js npm install log4js
## tests ## tests
Run the tests with `node tests.js`. They use the awesome [jspec](http://visionmedia.github.com/jspec) - 3.1.3 Tests now use [vows](http://vowsjs.org), run with `vows test/logging.js`. I am slowly porting the previous tests from jspec (run those with `node tests.js`), since jspec is no longer maintained.
## usage ## usage
See example.js: See example.js:
var log4js = require('log4js'); var log4js = require('log4js')(); //note the need to call the function
log4js.addAppender(log4js.consoleAppender()); log4js.addAppender(log4js.consoleAppender());
log4js.addAppender(log4js.fileAppender('logs/cheese.log'), 'cheese'); log4js.addAppender(log4js.fileAppender('logs/cheese.log'), 'cheese');
@ -40,7 +41,7 @@ Output
You can either configure the appenders and log levels manually (as above), or provide a You can either configure the appenders and log levels manually (as above), or provide a
configuration file (`log4js.configure('path/to/file.json')`). An example file can be found configuration file (`log4js.configure('path/to/file.json')`). An example file can be found
in spec/fixtures/log4js.json in test/log4js.json
## todo ## todo

File diff suppressed because it is too large Load Diff

View File

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

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' describe 'log4js'
before before
extend(context, { extend(context, {
log4js : require("log4js"), log4js : require("log4js")()
fs: require("fs"),
waitForWriteAndThenReadFile : function (filename) {
process.loop();
return fs.readFileSync(filename, "utf8");
}
}); });
end end
@ -18,34 +13,6 @@ describe 'log4js'
logger.addListener("log", function (logEvent) { event = logEvent; }); logger.addListener("log", function (logEvent) { event = logEvent; });
end 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' describe 'addAppender'
before_each before_each
appenderEvent = undefined; appenderEvent = undefined;
@ -177,24 +144,6 @@ describe 'log4js'
end end
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' describe 'logLevelFilter'
it 'should only pass log events greater than or equal to its own level' it 'should only pass log events greater than or equal to its own level'
@ -216,49 +165,6 @@ describe 'log4js'
end 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 end