Skip to content

Commit

Permalink
fix: File Transport not flush when logger.end() invoked
Browse files Browse the repository at this point in the history
  • Loading branch information
adoyle-h committed Nov 19, 2020
1 parent 3490860 commit b40232f
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lib/winston/transports/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ module.exports = class File extends TransportStream {
this._created = 0;
this._drain = false;
this._opening = false;
this._destOpened = false;
this._ending = false;

if (this.dirname) this._createLogDirIfNotExist(this.dirname);
Expand Down Expand Up @@ -522,8 +523,8 @@ module.exports = class File extends TransportStream {
*/
_endStream(callback = () => {}) {
if (this._dest) {
this._stream.unpipe(this._dest);
this._dest.end(() => {
this._stream.unpipe(this._dest);
this._cleanupStream(this._dest);
callback();
});
Expand All @@ -532,6 +533,32 @@ module.exports = class File extends TransportStream {
}
}

/**
* To fix issue https://github.com/winstonjs/winston/issues/228
* @private
* @param {function} cb Delaying the 'finish' event until callback is called
* @returns {undefined}
*/
_waitStreamsCreatedButNotPiped(cb) {
if (this._destOpened) {
return cb();
}

this.once('startPipe', () => {
this._dest.once('finish', () => {
cb();
});

this.close(() => {
this._endStream();
});
});
}

_final(cb) {
this._waitStreamsCreatedButNotPiped(cb);
}

/**
* Returns the WritableStream for the active file on this instance. If we
* should gzip the file then a zlib stream is returned.
Expand All @@ -549,8 +576,10 @@ module.exports = class File extends TransportStream {
.on('close', () => debug('close', dest.path, dest.bytesWritten))
.on('open', () => {
debug('file open ok', fullpath);
this._destOpened = true;
this.emit('open', fullpath);
source.pipe(dest);
this.emit('startPipe');

// If rotation occured during the open operation then we immediately
// start writing to a new PassThrough, begin opening the next file
Expand Down

0 comments on commit b40232f

Please sign in to comment.