Skip to content

Commit 776df3b

Browse files
committed
Bug fixes
1 parent 641c99c commit 776df3b

File tree

6 files changed

+82
-51
lines changed

6 files changed

+82
-51
lines changed

index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class RotatingFileStream {
2121
this._triggers = [];
2222
if (config.period) {
2323
const periodTrigger = new PeriodTrigger(config.period);
24-
this.triggers.push(periodTrigger);
24+
this._triggers.push(periodTrigger);
2525
if (config.rotateExisting) {
2626
this._rotator.once(FileStartTime, (startTime) => {
2727
periodTrigger.setInitialTime(startTime);
@@ -35,7 +35,9 @@ class RotatingFileStream {
3535
}
3636
this._rotatingLock = false;
3737
this._triggers.forEach((trigger) => {
38-
trigger.on(Rotate, this._rotate);
38+
trigger.on(Rotate, () => {
39+
this._rotate();
40+
});
3941
});
4042
this._rotator.on(NewFile, () => {
4143
this._queue.setFileHandle(this._rotator.getCurrentHandle());
@@ -52,7 +54,7 @@ class RotatingFileStream {
5254
this._rotatingLock = true;
5355
await this._queue.pause();
5456
const nextFileHandle = await this._rotator.rotate();
55-
this._triggers.forEach(trigger => trigger.rotated());
57+
this._triggers.forEach(trigger => trigger.newFile());
5658
this._queue.setFileHandle(nextFileHandle);
5759
this._rotatingLock = false;
5860
}

lib/fileRotator.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {createReadStream, createWriteStream} = require('fs');
55
const fs = require('fs/promises');
66
const pipe = promisify(pipeline);
77
const path = require('path');
8+
const {processSize} = require('../util/configProcessors');
89

910
const {EventEmitter} = require('events');
1011
const {NewFile, FileStartTime} = require('./customEvents');
@@ -15,6 +16,8 @@ class FileRotator extends EventEmitter {
1516
constructor(config) {
1617
super();
1718
this._startNewFile = config.startNewFile;
19+
this._totalFiles = config.totalFiles;
20+
this._totalSize = processSize(config.totalSize);
1821
this._gzip = config.gzip;
1922
this._path = config.path;
2023
this._initialised = false;
@@ -49,8 +52,8 @@ class FileRotator extends EventEmitter {
4952
if (this._gzip) {
5053
await this._gzipCurrentFile();
5154
}
52-
await this._moveIntermediateFiles();
5355
await this._deleteFiles();
56+
await this._moveIntermediateFiles();
5457
await this._initialiseNewFile();
5558
// Set by _initialiseNewFile
5659
return this._currentHandle;
@@ -113,13 +116,34 @@ class FileRotator extends EventEmitter {
113116
}
114117

115118
async _deleteFiles() {
116-
119+
const filesToDelete = [];
120+
let fileNumber = 0;
121+
let bytesTotal = 0;
122+
for (; ; fileNumber++) {
123+
const name = this._getFileName(fileNumber, true);
124+
try {
125+
const result = await fs.stat(name);
126+
bytesTotal += result.size;
127+
if ((this._totalSize && bytesTotal > this._totalSize) || (this._totalFiles && fileNumber + 1 > this._totalFiles)) {
128+
filesToDelete.push(name);
129+
}
130+
} catch (err) {
131+
if (err.code === 'ENOENT') {
132+
break;
133+
} else {
134+
throw err;
135+
}
136+
}
137+
}
138+
for (const file of filesToDelete) {
139+
await fs.unlink(file);
140+
}
117141
}
118142

119143
async _moveIntermediateFiles() {
120144
const filesToMove = [];
121145
let fileNumber = 0;
122-
for (;;) {
146+
for (; ; fileNumber++) {
123147
const name = this._getFileName(fileNumber, true);
124148
try {
125149
const result = await fs.stat(name);
@@ -131,7 +155,6 @@ class FileRotator extends EventEmitter {
131155
throw err;
132156
}
133157
}
134-
fileNumber += 1;
135158
}
136159
// Set to last file in sequence
137160
fileNumber -= 1;

lib/periodTrigger.js

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,6 @@
11
const {EventEmitter} = require('events');
22
const {Rotate} = require('./customEvents');
3-
4-
const parsePeriod = (period) => {
5-
const result = {
6-
num: 1,
7-
unit: 'd'
8-
};
9-
10-
// Parse `period`.
11-
if (period) {
12-
var crackedperiod = {
13-
hourly: '1h',
14-
daily: '1d',
15-
weekly: '1w',
16-
monthly: '1m',
17-
yearly: '1y'
18-
}[period] || period;
19-
20-
const [num, unit] = /^([1-9][0-9]*)([hdwmy]|ms)$/.exec(crackedperiod);
21-
22-
result.num = Number(num);
23-
result.unit = unit;
24-
}
25-
26-
return result;
27-
};
3+
const {processPeriod} = require('../util/configProcessors');
284

295
const getNextRotation = (period, lastRotation) => {
306
var date = new Date();
@@ -94,7 +70,7 @@ const getNextRotation = (period, lastRotation) => {
9470
class PeriodTrigger extends EventEmitter {
9571
constructor(period) {
9672
super();
97-
this._period = parsePeriod(period);
73+
this._period = processPeriod(period);
9874
this._task = null;
9975
this._rotateAt = null;
10076
}

lib/thresholdTrigger.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
11
const {EventEmitter} = require('events');
22
const {Rotate} = require('./customEvents');
3-
4-
const processThreshold = (threshold) => {
5-
const multipliers = {
6-
b: 1,
7-
k: 1024,
8-
m: 1024 * 1024,
9-
g: 1024 * 1024 * 1024
10-
};
11-
12-
if (typeof (threshold) === 'string') {
13-
const [num, unit] = /^([1-9][0-9]*)([bkmg])$/.exec(threshold).slice(1);
14-
return Number(num) * multipliers[unit];
15-
} else {
16-
return threshold;
17-
}
18-
};
3+
const {processSize} = require('../util/configProcessors');
194

205
class ThresholdTrigger extends EventEmitter {
216
constructor(threshold) {
227
super();
23-
this._threshold = processThreshold(threshold);
8+
this._threshold = processSize(threshold);
249
this._totalWritten = 0;
2510
}
2611

test/filerotate.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const bunyan = require('bunyan');
55

66
const RotatingFileStream = require('../index');
77
const testConfig = {
8-
path: 'foo.log',
8+
path: 'logs/foo.log',
99
totalFiles: 10,
1010
threshold: '1k',
1111
rotateExisting: true,

util/configProcessors.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const processSize = (size) => {
2+
const multipliers = {
3+
b: 1,
4+
k: 1024,
5+
m: 1024 * 1024,
6+
g: 1024 * 1024 * 1024
7+
};
8+
9+
if (typeof (size) === 'string') {
10+
const [num, unit] = /^([1-9][0-9]*)([bkmg])$/.exec(size).slice(1);
11+
return Number(num) * multipliers[unit];
12+
} else {
13+
return size;
14+
}
15+
};
16+
17+
const processPeriod = (period) => {
18+
const result = {
19+
num: 1,
20+
unit: 'd'
21+
};
22+
23+
// Parse `period`.
24+
if (period) {
25+
var crackedperiod = {
26+
hourly: '1h',
27+
daily: '1d',
28+
weekly: '1w',
29+
monthly: '1m',
30+
yearly: '1y'
31+
}[period] || period;
32+
33+
const [num, unit] = /^([1-9][0-9]*)([hdwmy]|ms)$/.exec(crackedperiod).slice(1);
34+
35+
result.num = Number(num);
36+
result.unit = unit;
37+
}
38+
39+
return result;
40+
};
41+
42+
module.exports = {
43+
processSize,
44+
processPeriod
45+
};

0 commit comments

Comments
 (0)