Skip to content

Commit 4cf25b5

Browse files
authored
emit new event when new log file is created (#220)
1 parent c7ec36d commit 4cf25b5

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ The DailyRotateFile transport can rotate files by minute, hour, day, month, year
5151
logger.info('Hello World!');
5252
```
5353

54-
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*).
54+
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*).
5555

5656
## LICENSE
5757
MIT

daily-rotate-file.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ var DailyRotateFile = function (options) {
9191
file_options: options.options ? options.options : {flags: 'a'}
9292
});
9393

94+
this.logStream.on('new', function (newFile) {
95+
self.emit('new', newFile);
96+
});
97+
9498
this.logStream.on('rotate', function (oldFile, newFile) {
9599
self.emit('rotate', oldFile, newFile);
96100
});

test/transport-tests.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ describe('winston/transports/daily-rotate-file', function () {
150150
}).to.throw();
151151
});
152152

153+
it('should raise the new event for a new log file', function (done) {
154+
this.transport.on('new', function (newFile) {
155+
expect(newFile).to.equal(filename);
156+
done();
157+
});
158+
159+
sendLogItem(this.transport, 'info', 'this message should write to the file');
160+
this.transport.close();
161+
});
162+
153163
describe('when setting zippedArchive', function () {
154164
it('should archive the log after rotating', function (done) {
155165
var self = this;
@@ -234,7 +244,6 @@ describe('winston/transports/daily-rotate-file', function () {
234244
done();
235245
});
236246
});
237-
this.transport.close();
238247
});
239248
});
240249
});

0 commit comments

Comments
 (0)