10000 added test for log roller, not written yet · Web5design/log4js-node@4406f21 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4406f21

Browse files
author
csausdev
committed
added test for log roller, not written yet
1 parent 8145e3b commit 4406f21

File tree

3 files changed

+91
-3
lines changed

3 files changed

+91
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Minimalist version:
2020
var log4js = require('log4js')();
2121
var logger = log4js.getLogger();
2222
logger.debug("Some debug messages");
23-
By default, log4js outputs to stdout with the basic layout, so for the above you would see:
23+
By default, log4js outputs to stdout with the coloured layout (thanks to [masylum](http://github.com/masylum)), so for the above you would see:
2424
[2010-01-17 11:43:37.987] [DEBUG] [default] - Some debug messages
2525

2626
See example.js:

lib/log4js.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"appenders": [
33
{
4-
"type": "console"
4+
"type": "console"
55
}
66
]
77
}

test/logging.js

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,94 @@ vows.describe('log4js').addBatch({
8080
}
8181
},
8282

83+
'fileAppender - with rolling based on size and number of files to keep': {
84+
topic: function() {
85+
var watchCb,
86+
filesOpened = [],
87+
filesClosed = [],
88+
filesRenamed = [],
89+
newFilenames = [],
90+
existingFiles = ['tests.log'],
91+
log4js = require('../lib/log4js')({
92+
watchFile: function(file, options, callback) {
93+
assert.equal(file, 'tests.log');
94+
assert.deepEqual(options, { persistent: true, interval: 30000 });
95+
assert.isFunction(callback);
96+
watchCb = callback;
97+
},
98+
openSync: function(file) {
99+
assert.equal(file, 'tests.log');
100+
filesOpened.push(file);
101+
return file;
102+
},
103+
statSync: function(file) {
104+
if (existingFiles.indexOf(file) < 0) {
105+
throw new Error("this file doesn't exist");
106+
} else {
107+
return true;
108+
}
109+
},
110+
renameSync: function(oldFile, newFile) {
111+
filesRenamed.push(oldFile);
112+
existingFiles.push(newFile);
113+
},
114+
closeSync: function(file) {
115+
//it should always be closing tests.log
116+
assert.equal(file, 'tests.log');
117+
filesClosed.push(file);
118+
}
119+
});
120+
var appender = log4js.fileAppender('tests.log', log4js.messagePassThroughLayout, 1024, 2, 30);
121+
return [watchCb, filesOpened, filesClosed, filesRenamed, existingFiles];
122+
},
123+
124+
'should close current log file, rename all old ones, open new one on rollover': function(args) {
125+
var watchCb = args[0], filesOpened = args[1], filesClosed = args[2], filesRenamed = args[3], existingFiles = args[4];
126+
assert.isFunction(watchCb);
127+
//tell the watchCb that the file is below the threshold
128+
watchCb({ size: 891 }, { size: 0 });
129+
//filesOpened should still be the first one.
130+
assert.length(filesOpened, 1);
131+
//tell the watchCb that the file is now over the threshold
132+
watchCb({ size: 1053 }, { size: 891 });
133+
//it should have closed the first log file.
134+
assert.length(filesClosed, 1);
135+
//it should have renamed the previous log file
136+
assert.length(filesRenamed, 1);
137+
//and we should have two files now
138+
assert.length(existingFiles, 2);
139+
assert.deepEqual(existingFiles, ['tests.log', 'tests.log.1']);
140+
//and opened a new log file.
141+
assert.length(filesOpened, 2);
142+
143+
//now tell the watchCb that we've flipped over the threshold again
144+
watchCb({ size: 1025 }, { size: 123 });
145+
//it should have closed the old file
146+
assert.length(filesClosed, 2);
147+
//it should have renamed both the old log file, and the previous '.1' file
148+
assert.length(filesRenamed, 3);
149+
assert.deepEqual(filesRenamed, ['tests.log', 'tests.log', 'tests.log.1' ]);
150+
//it should have renamed 2 more file
151+
assert.length(existingFiles, 2);
152+
assert.deepEqual(existingFiles, ['tests.log', 'tests.log.2', 'tests.log.1']);
153+
//and opened a new log file
154+
assert.length(filesOpened, 3);
155+
156+
//tell the watchCb we've flipped again.
157+
watchCb({ size: 1024 }, { size: 234 });
158+
//close the old one again.
159+
assert.length(filesClosed, 3);
160+
//it should have renamed the old log file and the 2 backups, with the last one being overwritten.
161+
assert.length(filesRenamed, 5);
162+
assert.deepEqual(filesRenamed, ['tests.log', 'tests.log', 'tests.log.1', 'tests.log', 'tests.log.1' ]);
163+
//it should have renamed 2 more files
164+
assert.length(existingFiles, 5);
165+
assert.deepEqual(existingFiles, ['tests.log', 'tests.log.2', 'tests.log.1', 'tests.log.2', 'tests.log.1']);
166+
//and opened a new log file
167+
assert.length(filesOpened, 4);
168+
}
169+
},
170+
83171
'configure' : {
84172
topic: function() {
85173
var messages = {}, fakeFS = {
@@ -90,7 +178,7 @@ vows.describe('log4js').addBatch({
90178
if (!messages.hasOwnProperty(file)) {
91179
messages[file] = [];
92180
}
93-
messages[file].push(message);
181+
messages[file].push(message);
94182
},
95183
readFileSync: function(file, encoding) {
96184
return require('fs').readFileSync(file, encoding);

0 commit comments

Comments
 (0)
0