diff --git a/lib/winston/transports/file.js b/lib/winston/transports/file.js index 60af7f580..b38584bc2 100644 --- a/lib/winston/transports/file.js +++ b/lib/winston/transports/file.js @@ -88,6 +88,7 @@ module.exports = class File extends TransportStream { this._drain = false; this._opening = false; this._ending = false; + this._writeStreams = []; if (this.dirname) this._createLogDirIfNotExist(this.dirname); this.open(); @@ -400,6 +401,9 @@ module.exports = class File extends TransportStream { debug('stat done: %s { size: %s }', this.filename, size); this._size = size; this._dest = this._createStream(this._stream); + + this._writeStreams.push(this._dest); + this._opening = false; this.once('open', () => { if (this._stream.eventNames().includes('rotate')) { @@ -503,6 +507,12 @@ module.exports = class File extends TransportStream { _cleanupStream(stream) { stream.removeListener('error', this._onError); + let streamToClose = this._writeStreams.pop(); + while (streamToClose) { + streamToClose.destroy(); + streamToClose = this._writeStreams.pop(); + } + return stream; }