8000 allow for zero backup - in sync with https://github.com/log4js-node/s… · log4js-node/log4js-node@ac599e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac599e4

Browse files
committed
allow for zero backup - in sync with log4js-node/streamroller#74
**Important** It is also to note the file does not roll within itself (truncate its older entry for newer entry). It truncates all and appends only the new entry. ```javascript var rollers = require('streamroller'); var stream = new rollers.RollingFileStream('myfile', 6, 0); stream.write("abc"); // add as first row stream.write("def"); // add as second row stream.write("ghi"); // truncate all and add as first row stream.end(); ``` Output: ``` myfile - ghi ```
1 parent 53248cd commit ac599e4

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

lib/appenders/file.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ function openTheStream(file, fileSize, numFiles, options) {
4545
*/
4646
function fileAppender(file, layout, logSize, numBackups, options, timezoneOffset) {
4747
file = path.normalize(file);
48-
numBackups = numBackups === undefined ? 5 : numBackups;
49-
// there has to be at least one backup if logSize has been specified
50-
numBackups = numBackups === 0 ? 1 : numBackups;
48+
numBackups = (!numBackups && numBackups !== 0) ? 5 : numBackups;
5149

5250
debug(
5351
'Creating file appender (',

lib/appenders/fileSync.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class RollingFileSync {
3030

3131
this.filename = filename;
3232
this.size = size;
33-
this.backups = backups || 1;
33+
this.backups = backups;
3434
this.options = options;
3535
this.currentSize = 0;
3636

@@ -80,7 +80,9 @@ class RollingFileSync {
8080
function increaseFileIndex(fileToRename) {
8181
const idx = index(fileToRename);
8282
debug(`Index of ${fileToRename} is ${idx}`);
83-
if (idx < that.backups) {
83+
if (that.backups === 0) {
84+
fs.truncateSync(filename, 0);
85+
} else if (idx < that.backups) {
8486
// on windows, you can get a EEXIST error if you rename a file to an existing file
8587
// so, we'll try to delete the file we're renaming to first
8688
try {
@@ -146,9 +148,7 @@ class RollingFileSync {
146148
function fileAppender(file, layout, logSize, numBackups, timezoneOffset, options) {
147149
debug('fileSync appender created');
148150
file = path.normalize(file);
149-
numBackups = numBackups === undefined ? 5 : numBackups;
150-
// there has to be at least one backup if logSize has been specified
151-
numBackups = numBackups === 0 ? 1 : numBackups;
151+
numBackups = (!numBackups && numBackups !== 0) ? 5 : numBackups;
152152

153153
function openTheStream(filePath, fileSize, numFiles) {
154154
let stream;

test/tap/fileAppender-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ test("log4js fileAppender", batch => {
8282

8383
t.tearDown(async () => {
8484
await new Promise(resolve => log4js.shutdown(resolve));
85-
await Promise.all([removeFile(testFile), removeFile(`${testFile}.1`)]);
85+
await removeFile(testFile);
8686
});
87-
await Promise.all([removeFile(testFile), removeFile(`${testFile}.1`)]);
87+
await removeFile(testFile);
8888

8989
// log file of 100 bytes maximum, no backups
9090
log4js.configure({
@@ -113,7 +113,7 @@ test("log4js fileAppender", batch => {
113113
const logFiles = files.filter(file =>
114114
file.includes("fa-maxFileSize-test.log")
115115
);
116-
t.equal(logFiles.length, 2, "should be 2 files");
116+
t.equal(logFiles.length, 1, "should be 1 file");
117117
t.end();
118118
});
119119

@@ -158,7 +158,7 @@ test("log4js fileAppender", batch => {
158158
const logFiles = files.filter(file =>
159159
file.includes("fa-maxFileSize-unit-test.log")
160160
);
161-
t.equal(logFiles.length, 2, "should be 2 files");
161+
t.equal(logFiles.length, 1, "should be 1 file");
162162
t.end();
163163
});
164164

test/tap/fileSyncAppender-test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ test("log4js fileSyncAppender", batch => {
4343
const testFile = path.join(__dirname, "/fa-maxFileSize-sync-test.log");
4444
const logger = log4js.getLogger("max-file-size");
4545
remove(testFile);
46-
remove(`${testFile}.1`);
4746

4847
t.tearDown(() => {
4948
remove(testFile);
50-
remove(`${testFile}.1`);
5149
});
5250

5351
// log file of 100 bytes maximum, no backups
@@ -77,12 +75,12 @@ test("log4js fileSyncAppender", batch => {
7775
});
7876
});
7977

80-
t.test("there should be two test files", assert => {
78+
t.test("there should be one test files", assert => {
8179
fs.readdir(__dirname, (err, files) => {
8280
const logFiles = files.filter(file =>
8381
file.includes("fa-maxFileSize-sync-test.log")
8482
);
85-
assert.equal(logFiles.length, 2);
83+
assert.equal(logFiles.length, 1);
8684
assert.end();
8785
});
8886
});
@@ -128,12 +126,12 @@ test("log4js fileSyncAppender", batch => {
128126
});
129127
});
130128

131-
t.test("there should be two test files", assert => {
129+
t.test("there should be one test file", assert => {
132130
fs.readdir(__dirname, (err, files) => {
133131
const logFiles = files.filter(file =>
134132
file.includes("fa-maxFileSize-unit-sync-test.log")
135133
);
136-
assert.equal(logFiles.length, 2);
134+
assert.equal(logFiles.length, 1);
137135
assert.end();
138136
});
139137
});

0 commit comments

Comments
 (0)
0