diff --git a/README.md b/README.md index b109b44..988b8fd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/daily-rotate-file.js b/daily-rotate-file.js index 0c1a21a..f674f44 100644 --- a/daily-rotate-file.js +++ b/daily-rotate-file.js @@ -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); diff --git a/package-lock.json b/package-lock.json index b8c41af..b92e6fd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -539,9 +539,9 @@ } }, "file-stream-rotator": { - "version": "0.4.1", - "resolved": "https://registry.npmjs.org/file-stream-rotator/-/file-stream-rotator-0.4.1.tgz", - "integrity": "sha512-W3aa3QJEc8BS2MmdVpQiYLKHj3ijpto1gMDlsgCRSKfIUe6MwkcpODGPQ3vZfb0XvCeCqlu9CBQTN7oQri2TZQ==", + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/file-stream-rotator/-/file-stream-rotator-0.5.3.tgz", + "integrity": "sha512-Di94420V8A0U/0fs4hxMD3naxN6IIeV6G2fQ2gCt5pjJfsH2xUtWKt4EGcy6xaNZ10705rSrqi4U3ijrreo3YQ==", "requires": { "moment": "^2.11.2" } diff --git a/package.json b/package.json index e30fbf9..080ae55 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/transport-tests.js b/test/transport-tests.js index a346c0b..d953f88 100644 --- a/test/transport-tests.js +++ b/test/transport-tests.js @@ -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'; @@ -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(); }); }); });