Skip to content

Commit 090f7a9

Browse files
author
Matt Berther
committed
raise logRemoved event when log file is removed from file system
1 parent aa820d2 commit 090f7a9

File tree

5 files changed

+32
-7
lines changed

5 files changed

+32
-7
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ The DailyRotateFile transport can rotate files by minute, hour, day, month, year
5454
logger.info('Hello World!');
5555
```
5656

57-
This transport emits three custom events: *new*, *rotate*, and *archive*. You can listen for the *new* custom event, which is fired when a new log file is created. The new event will pass one parameter to the callback (*newFilename*). You can listen for the *rotate* custom event, which is fired when the log file is rotated. The rotate event will pass two parameters to the callback (*oldFilename*, *newFilename*). You can also listen for the *archive* custom event, which is fired when the log file is archived. The archive event will pass one parameter to the callback (*zipFilename*).
57+
This transport emits the following custom events:
58+
59+
* *new*: fired when a new log file is created. This event will pass one parameter to the callback (*newFilename*).
60+
* *rotate*: fired when the log file is rotated. This event will pass two parameters to the callback (*oldFilename*, *newFilename*).
61+
* *archive*: fired when the log file is archived. This event will pass one parameter to the callback (*zipFilename*).
62+
* *logRemoved*: fired when a log file is removed from the file system. This event will pass one parameter to the callback (*removedFilename*).
5863

5964
## LICENSE
6065
MIT

daily-rotate-file.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ var DailyRotateFile = function (options) {
9797
self.emit('rotate', oldFile, newFile);
9898
});
9999

100+
this.logStream.on('logRemoved', function (params) {
101+
self.emit('logRemoved', params.name);
102+
});
103+
100104
if (options.zippedArchive) {
101105
this.logStream.on('rotate', function (oldFile) {
102106
var oldFileExist = fs.existsSync(oldFile);

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"rimraf": "2.6.3"
4141
},
4242
"dependencies": {
43-
"file-stream-rotator": "^0.4.1",
43+
"file-stream-rotator": "~0.5.0",
4444
"object-hash": "^1.3.0",
4545
"triple-beam": "^1.3.0",
4646
"winston-transport": "^4.2.0"

test/transport-tests.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,25 @@ describe('winston/transports/daily-rotate-file', function () {
154154
this.transport.close();
155155
});
156156

157+
it('should raise the logRemoved event when pruning old log files', function (done) {
158+
var opts = Object.assign({}, options);
159+
opts.maxSize = '1k';
160+
opts.maxFiles = 1;
161+
162+
this.transport = new DailyRotateFile(opts);
163+
164+
this.transport.on('logRemoved', function (removedFilename) {
165+
expect(removedFilename).to.equal(filename);
166+
done();
167+
});
168+
169+
sendLogItem(this.transport, 'info', randomString(1056));
170+
sendLogItem(this.transport, 'info', randomString(1056));
171+
this.transport.close();
172+
});
173+
157174
describe('when setting zippedArchive', function () {
158175
it('should archive the log after rotating', function (done) {
159-
var self = this;
160176
var opts = Object.assign({}, options);
161177
opts.zippedArchive = true;
162178
opts.maxSize = '1k';
@@ -173,7 +189,7 @@ describe('winston/transports/daily-rotate-file', function () {
173189
});
174190
sendLogItem(this.transport, 'info', randomString(1056));
175191
sendLogItem(this.transport, 'info', randomString(1056));
176-
self.transport.close();
192+
this.transport.close();
177193
});
178194
});
179195
});

0 commit comments

Comments
 (0)