8000 added a log rolling function to file appender · lalitkapoor/log4js-node@fb8b455 · GitHub
[go: up one dir, main page]

Skip to content

Commit fb8b455

Browse files
author
csausdev
committed
added a log rolling function to file appender
1 parent 0258fda commit fb8b455

File tree

2 files changed

+62
-11
lines changed

2 files changed

+62
-11
lines changed

lib/log4js.js

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,22 +342,69 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
342342
}
343343

344344
/**
345-
* File Appender writing the logs to a text file.
345+
* File Appender writing the logs to a text file. Supports rolling of logs by size.
346346
*
347347
* @param file file log messages will be written to
348348
* @param layout a function that takes a logevent and returns a string (defaults to basicLayout).
349+
* @param logSize - the maximum size (in bytes) for a log file, if not provided then logs won't be rotated.
350+
* @param numBackups - the number of log files to keep after logSize has been reached (default 5)
351+
* @param filePollInterval - the time in seconds between file size checks (default 30s)
349352
*/
350-
function fileAppender (file, layout) {
353+
function fileAppender (file, layout, logSize, numBackups, filePollInterval) {
351354
layout = layout || basicLayout;
352355
//syncs are generally bad, but we need
353356
//the file to be open before we start doing any writing.
354-
var logFile = fs.openSync(file, 'a', 0644);
357+
var logFile = fs.openSync(file, 'a', 0644);
358+
359+
if (logSize > 0) {
360+
setupLogRolling(logFile, file, logSize, numBackups || 5, (filePollInterval * 1000) || 30000);
361+
}
355362

356363
return function(loggingEvent) {
357364
fs.write(logFile, layout(loggingEvent)+'\n', null, "utf8");
358365
};
359366
}
360367

368+
function setupLogRolling (logFile, filename, logSize, numBackups, filePollInterval) {
369+
fs.watchFile(filename,
370+
{
371+
persistent: false,
372+
interval: filePollInterval
373+
},
374+
function (curr, prev) {
375+
if (curr.size >= logSize) {
376+
rollThatLog(logFile, filename, numBackups);
377+
}
378+
}
379+
);
380+
}
381+
382+
function rollThatLog (logFile, filename, numBackups) {
383+
//first close the current one.
384+
fs.closeSync(logFile);
385+
//roll the backups (rename file.n-1 to file.n, where n <= numBackups)
386+
for (var i=numBackups; i > 0; i--) {
387+
if (i > 1) {
388+
if (fileExists(filename + '.' + (i-1))) {
389+
fs.renameSync(filename+'.'+(i-1), filename+'.'+i);
390+
}
391+
} else {
392+
fs.renameSync(filename, filename+'.1');
393+
}
394+
}
395+
//open it up again
396+
logFile = fs.openSync(filename, 'a', 0644);
397+
}
398+
399+
function fileExists (filename) {
400+
try {
401+
fs.statSync(filename);
402+
return true;
403+
} catch (e) {
404+
return false;
405+
}
406+
}
407+
361408
function logLevelFilter (levelString, appender) {
362409
var level = Level.toLevel(levelString);
363410
return function(logEvent) {

test/logging.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ vows.describe('log4js').addBatch({
6161
assert.isNull(arguments[2]);
6262
assert.equal(arguments[3], "utf8");
6363
logmessages.push(arguments[1]);
64-
}
64+
},
65+
watchFile: function() {
66+
throw new Error("watchFile should not be called if logSize is not defined");
67+
}
6568
},
6669
log4js = require('../lib/log4js')(fakeFS);
6770
log4js.clearAppenders();
@@ -91,7 +94,8 @@ vows.describe('log4js').addBatch({
9194
log4js = require('../lib/log4js')({
9295
watchFile: function(file, options, callback) {
9396
assert.equal(file, 'tests.log');
94-
assert.deepEqual(options, { persistent: true, interval: 30000 });
97+
assert.equal(options.persistent, false);
98+
assert.equal(options.interval, 30000);
9599
assert.isFunction(callback);
96100
watchCb = callback;
97101
},
@@ -146,10 +150,10 @@ vows.describe('log4js').addBatch({
146150
assert.length(filesClosed, 2);
147151
//it should have renamed both the old log file, and the previous '.1' file
148152
assert.length(filesRenamed, 3);
149-
assert.deepEqual(filesRenamed, ['tests.log', 'tests.log', 'tests.log.1' ]);
153+
assert.deepEqual(filesRenamed, ['tests.log', 'tests.log.1', 'tests.log' ]);
150154
//it should have renamed 2 more file
151-
assert.length(existingFiles, 2);
152-
assert.deepEqual(existingFiles, ['tests.log', 'tests.log.2', 'tests.log.1']);
155+
assert.length(existingFiles, 4);
156+
assert.deepEqual(existingFiles, ['tests.log', 'tests.log.1', 'tests.log.2', 'tests.log.1']);
153157
//and opened a new log file
154158
assert.length(filesOpened, 3);
155159

@@ -159,10 +163,10 @@ vows.describe('log4js').addBatch({
159163
assert.length(filesClosed, 3);
160164
//it should have renamed the old log file and the 2 backups, with the last one being overwritten.
161165
assert.length(filesRenamed, 5);
162-
assert.deepEqual(filesRenamed, ['tests.log', 'tests.log', 'tests.log.1', 'tests.log', 'tests.log.1' ]);
166+
assert.deepEqual(filesRenamed, ['tests.log', 'tests.log.1', 'tests.log', 'tests.log.1', 'tests.log' ]);
163167
//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']);
168+
assert.length(existingFiles, 6);
169+
assert.deepEqual(existingFiles, ['tests.log', 'tests.log.1', 'tests.log.2', 'tests.log.1', 'tests.log.2', 'tests.log.1']);
166170
//and opened a new log file
167171
assert.length(filesOpened, 4);
168172
}

0 commit comments

Comments
 (0)
0