8000 Added log rolling to config files · wxqGitHub/log4js-node@966b8ce · GitHub
[go: up one dir, main page]

Skip to content

Commit 966b8ce

Browse files
author
csausdev
committed
Added log rolling to config files
1 parent 75b9e82 commit 966b8ce

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This is a conversion of the [log4js](http://log4js.berlios.de/index.html)
44
framework to work with [node](http://nodejs.org). I've mainly stripped out the browser-specific code
5-
and tidied up some of the javascript.
5+
and tidied up some of the javascript. It includes a basic file logger, with log rolling based on file size.
66

77
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).
88

@@ -47,13 +47,11 @@ Output
4747
## configuration
4848

4949
You can either configure the appenders and log levels manually (as above), or provide a
50-
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).
51-
An example file can be found in test/log4js.json
50+
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).
51+
An example file can be found in `test/log4js.json`. An example config file with log rolling is in `test/with-log-rolling.json`
5252

5353
## todo
5454

55-
I need to make a RollingFileAppender, which will do log rotation.
56-
5755
patternLayout has no tests. This is mainly because I haven't found a use for it yet,
5856
and am not entirely sure what it was supposed to do. It is more-or-less intact from
5957
the original log4js.

lib/log4js.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
7171
if (config.layout) {
7272
layout = layoutMakers[config.layout.type](config.layout);
7373
}
74-
return fileAppender(config.filename, layout);
74+
return fileAppender(config.filename, layout, config.maxLogSize, config.backups, config.pollInterval);
7575
},
7676
"console": function(config) {
7777
var layout;

test/logging.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,10 @@ vows.describe('log4js').addBatch({
186186
},
187187
readFileSync: function(file, encoding) {
188188
return require('fs').readFileSync(file, encoding);
189-
}
189+
},
190+
watchFile: function(file) {
191+
messages.watchedFile = file;
192+
}
190193
},
191194
log4js = require('../lib/log4js')(fakeFS);
192195
return [ log4js, messages ];
@@ -220,7 +223,13 @@ vows.describe('log4js').addBatch({
220223
assert.length(messages['tmp-tests-warnings.log'], 2);
221224
assert.deepEqual(messages['tmp-tests.log'], ['main\n','both\n','both\n','main\n']);
222225
assert.deepEqual(messages['tmp-tests-warnings.log'], ['both\n','both\n']);
223-
}
226+
},
227+
'should handle fileAppender with log rolling' : function(args) {
228+
var log4js = args[0], messages = args[1];
229+
delete messages['tmp-test.log'];
230+
log4js.configure('test/with-log-rolling.json');
231+
assert.equal(messages.watchedFile, 'tmp-test.log');
232+
}
224233
},
225234

226235
'with no appenders defined' : {

test/with-log-rolling.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"appenders": [
3+
{
4+
"type": "file",
5+
"filename": "tmp-test.log",
6+
"maxLogSize": 1024,
7+
"backups": 3,
8+
"pollInterval": 15
9+
}
10+
]
11+
}

0 commit comments

Comments
 (0)
0