Added log rolling to config files

This commit is contained in:
csausdev 2010-12-06 18:23:43 +11:00
parent 75b9e82cac
commit 966b8ced4f
4 changed files with 26 additions and 8 deletions

View File

@ -2,7 +2,7 @@
This is a conversion of the [log4js](http://log4js.berlios.de/index.html) 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. It includes a basic file logger, with log rolling based on file size.
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). 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).
@ -47,13 +47,11 @@ Output
## configuration ## configuration
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')`) explicitly, or just let log4js look for a file called `log4js.json` (it looks in the current directory first, then the require paths, and finally looks for the default config included in the same directory as the log4js.js file). configuration file (`log4js.configure('path/to/file.json')`) explicitly, or just let log4js look for a file called `log4js.json` (it looks in the current directory first, then the require paths, and finally looks for the default config included in the same directory as the `log4js.js` file).
An example file can be found in test/log4js.json An example file can be found in `test/log4js.json`. An example config file with log rolling is in `test/with-log-rolling.json`
## todo ## todo
I need to make a RollingFileAppender, which will do log rotation.
patternLayout has no tests. This is mainly because I haven't found a use for it yet, patternLayout has no tests. This is mainly because I haven't found a use for it yet,
and am not entirely sure what it was supposed to do. It is more-or-less intact from and am not entirely sure what it was supposed to do. It is more-or-less intact from
the original log4js. the original log4js.

View File

@ -71,7 +71,7 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
if (config.layout) { if (config.layout) {
layout = layoutMakers[config.layout.type](config.layout); layout = layoutMakers[config.layout.type](config.layout);
} }
return fileAppender(config.filename, layout); return fileAppender(config.filename, layout, config.maxLogSize, config.backups, config.pollInterval);
}, },
"console": function(config) { "console": function(config) {
var layout; var layout;

View File

@ -186,7 +186,10 @@ vows.describe('log4js').addBatch({
}, },
readFileSync: function(file, encoding) { readFileSync: function(file, encoding) {
return require('fs').readFileSync(file, encoding); return require('fs').readFileSync(file, encoding);
} },
watchFile: function(file) {
messages.watchedFile = file;
}
}, },
log4js = require('../lib/log4js')(fakeFS); log4js = require('../lib/log4js')(fakeFS);
return [ log4js, messages ]; return [ log4js, messages ];
@ -220,7 +223,13 @@ vows.describe('log4js').addBatch({
assert.length(messages['tmp-tests-warnings.log'], 2); assert.length(messages['tmp-tests-warnings.log'], 2);
assert.deepEqual(messages['tmp-tests.log'], ['main\n','both\n','both\n','main\n']); assert.deepEqual(messages['tmp-tests.log'], ['main\n','both\n','both\n','main\n']);
assert.deepEqual(messages['tmp-tests-warnings.log'], ['both\n','both\n']); assert.deepEqual(messages['tmp-tests-warnings.log'], ['both\n','both\n']);
} },
'should handle fileAppender with log rolling' : function(args) {
var log4js = args[0], messages = args[1];
delete messages['tmp-test.log'];
log4js.configure('test/with-log-rolling.json');
assert.equal(messages.watchedFile, 'tmp-test.log');
}
}, },
'with no appenders defined' : { 'with no appenders defined' : {

View File

@ -0,0 +1,11 @@
{
"appenders": [
{
"type": "file",
"filename": "tmp-test.log",
"maxLogSize": 1024,
"backups": 3,
"pollInterval": 15
}
]
}