Skip to content

Commit

Permalink
raise logRemoved event when log file is removed from file system
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Berther committed Sep 4, 2019
1 parent aa820d2 commit 090f7a9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ The DailyRotateFile transport can rotate files by minute, hour, day, month, year
logger.info('Hello World!');
```

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*).
This transport emits the following custom events:

* *new*: fired when a new log file is created. This event will pass one parameter to the callback (*newFilename*).
* *rotate*: fired when the log file is rotated. This event will pass two parameters to the callback (*oldFilename*, *newFilename*).
* *archive*: fired when the log file is archived. This event will pass one parameter to the callback (*zipFilename*).
* *logRemoved*: fired when a log file is removed from the file system. This event will pass one parameter to the callback (*removedFilename*).

## LICENSE
MIT
Expand Down
4 changes: 4 additions & 0 deletions daily-rotate-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ var DailyRotateFile = function (options) {
self.emit('rotate', oldFile, newFile);
});

this.logStream.on('logRemoved', function (params) {
self.emit('logRemoved', params.name);
});

if (options.zippedArchive) {
this.logStream.on('rotate', function (oldFile) {
var oldFileExist = fs.existsSync(oldFile);
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"rimraf": "2.6.3"
},
"dependencies": {
"file-stream-rotator": "^0.4.1",
"file-stream-rotator": "~0.5.0",
"object-hash": "^1.3.0",
"triple-beam": "^1.3.0",
"winston-transport": "^4.2.0"
Expand Down
20 changes: 18 additions & 2 deletions test/transport-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,25 @@ describe('winston/transports/daily-rotate-file', function () {
this.transport.close();
});

it('should raise the logRemoved event when pruning old log files', function (done) {
var opts = Object.assign({}, options);
opts.maxSize = '1k';
opts.maxFiles = 1;

this.transport = new DailyRotateFile(opts);

this.transport.on('logRemoved', function (removedFilename) {
expect(removedFilename).to.equal(filename);
done();
});

sendLogItem(this.transport, 'info', randomString(1056));
sendLogItem(this.transport, 'info', randomString(1056));
this.transport.close();
});

describe('when setting zippedArchive', function () {
it('should archive the log after rotating', function (done) {
var self = this;
var opts = Object.assign({}, options);
opts.zippedArchive = true;
opts.maxSize = '1k';
Expand All @@ -173,7 +189,7 @@ describe('winston/transports/daily-rotate-file', function () {
});
sendLogItem(this.transport, 'info', randomString(1056));
sendLogItem(this.transport, 'info', randomString(1056));
self.transport.close();
this.transport.close();
});
});
});
Expand Down

0 comments on commit 090f7a9

Please sign in to comment.