Skip to content

Commit

Permalink
emit new event when new log file is created (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattberther committed Apr 27, 2019
1 parent c7ec36d commit 4cf25b5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -51,7 +51,7 @@ The DailyRotateFile transport can rotate files by minute, hour, day, month, year
logger.info('Hello World!');
```

You can listen for the *rotate* custom event. The rotate event will pass two parameters to the callback (*oldFilename*, *newFilename*). You can also listen for the *archive* custom event. The archive event will pass one parameter to the callback (*zipFilename*).
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*).

## LICENSE
MIT
Expand Down
4 changes: 4 additions & 0 deletions daily-rotate-file.js
Expand Up @@ -91,6 +91,10 @@ var DailyRotateFile = function (options) {
file_options: options.options ? options.options : {flags: 'a'}
});

this.logStream.on('new', function (newFile) {
self.emit('new', newFile);
});

this.logStream.on('rotate', function (oldFile, newFile) {
self.emit('rotate', oldFile, newFile);
});
Expand Down
11 changes: 10 additions & 1 deletion test/transport-tests.js
Expand Up @@ -150,6 +150,16 @@ describe('winston/transports/daily-rotate-file', function () {
}).to.throw();
});

it('should raise the new event for a new log file', function (done) {
this.transport.on('new', function (newFile) {
expect(newFile).to.equal(filename);
done();
});

sendLogItem(this.transport, 'info', 'this message should write to the file');
this.transport.close();
});

describe('when setting zippedArchive', function () {
it('should archive the log after rotating', function (done) {
var self = this;
Expand Down Expand Up @@ -234,7 +244,6 @@ describe('winston/transports/daily-rotate-file', function () {
done();
});
});
this.transport.close();
});
});
});
Expand Down

0 comments on commit 4cf25b5

Please sign in to comment.