Skip to content

Commit

Permalink
Attempt at fixing the issues with ExceptionHandler in #1289 (#1355)
Browse files Browse the repository at this point in the history
  • Loading branch information
DABH authored and indexzero committed Dec 26, 2018
1 parent 44e178f commit 1873058
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
7 changes: 3 additions & 4 deletions lib/winston/exception-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,6 @@ module.exports = class ExceptionHandler {

// Log to all transports attempting to listen for when they are completed.
asyncForEach(handlers, (handler, next) => {
// TODO: Change these to the correct WritableStream events so that we
// wait until exit.
const done = once(next);
const transport = handler.transport || handler;

Expand All @@ -214,9 +212,10 @@ module.exports = class ExceptionHandler {
};
}

transport.once('logged', onDone('logged'));
transport._ending = true;
transport.once('finish', onDone('finished'));
transport.once('error', onDone('error'));
}, gracefulExit);
}, () => doExit && gracefulExit());

this.logger.log(info);

Expand Down
34 changes: 28 additions & 6 deletions lib/winston/transports/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,26 @@ module.exports = class File extends TransportStream {
this._created = 0;
this._drain = false;
this._opening = false;
this._ending = false;

this.open();
}

finishIfEnding() {
if (this._ending) {
if (this._opening) {
this.once('open', () => {
this._stream.once('finish', () => this.emit('finish'));
setImmediate(() => this._stream.end());
});
} else {
this._stream.once('finish', () => this.emit('finish'));
setImmediate(() => this._stream.end());
}
}
}


/**
* Core logging method exposed to Winston. Metadata is optional.
* @param {Object} info - TODO: add param description.
Expand Down Expand Up @@ -177,6 +193,8 @@ module.exports = class File extends TransportStream {

debug('written', written, this._drain);

this.finishIfEnding();

return written;
}

Expand Down Expand Up @@ -503,12 +521,16 @@ module.exports = class File extends TransportStream {
* @param {function} callback - Callback for when the current file has closed.
* @private
*/
_endStream(callback) {
this._stream.unpipe(this._dest);
this._dest.end(() => {
this._cleanupStream(this._dest);
callback();
});
_endStream(callback = () => {}) {
if (this._dest) {
this._stream.unpipe(this._dest);
this._dest.end(() => {
this._cleanupStream(this._dest);
callback();
});
} else {
callback(); // eslint-disable-line callback-return
}
}

/**
Expand Down

0 comments on commit 1873058

Please sign in to comment.