From 966b8ced4f4a6c33f233b1b7e68cb69da2696409 Mon Sep 17 00:00:00 2001 From: csausdev Date: Mon, 6 Dec 2010 18:23:43 +1100 Subject: [PATCH] Added log rolling to config files --- README.md | 8 +++----- lib/log4js.js | 2 +- test/logging.js | 13 +++++++++++-- test/with-log-rolling.json | 11 +++++++++++ 4 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 test/with-log-rolling.json diff --git a/README.md b/README.md index a251877..82ad54b 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ 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 -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). @@ -47,13 +47,11 @@ Output ## configuration 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). -An example file can be found in test/log4js.json +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 config file with log rolling is in `test/with-log-rolling.json` ## 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, and am not entirely sure what it was supposed to do. It is more-or-less intact from the original log4js. diff --git a/lib/log4js.js b/lib/log4js.js index 5a83fca..2c47dec 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -71,7 +71,7 @@ module.exports = function (fileSystem, standardOutput, configPaths) { if (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) { var layout; diff --git a/test/logging.js b/test/logging.js index 23bf0f4..8045698 100644 --- a/test/logging.js +++ b/test/logging.js @@ -186,7 +186,10 @@ vows.describe('log4js').addBatch({ }, readFileSync: function(file, encoding) { return require('fs').readFileSync(file, encoding); - } + }, + watchFile: function(file) { + messages.watchedFile = file; + } }, log4js = require('../lib/log4js')(fakeFS); return [ log4js, messages ]; @@ -220,7 +223,13 @@ vows.describe('log4js').addBatch({ 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-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' : { diff --git a/test/with-log-rolling.json b/test/with-log-rolling.json new file mode 100644 index 0000000..1d745ca --- /dev/null +++ b/test/with-log-rolling.json @@ -0,0 +1,11 @@ +{ + "appenders": [ + { + "type": "file", + "filename": "tmp-test.log", + "maxLogSize": 1024, + "backups": 3, + "pollInterval": 15 + } + ] +} \ No newline at end of file