Skip to content

Commit

Permalink
Fixes issue winstonjs#1194
Browse files Browse the repository at this point in the history
  • Loading branch information
rc-mz committed Mar 15, 2018
1 parent d795dc9 commit d43919f
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions lib/winston/transports/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ var File = module.exports = function (options) {
//
// Bind this context for listener functions
//
this._onDrain = this._onDrain.bind(this);
this._onError = this._onError.bind(this);

if (options.filename || options.dirname) {
Expand Down Expand Up @@ -90,7 +89,6 @@ var File = module.exports = function (options) {
this._size = 0;
this._created = 0;
this._drains = 0;
this._next = noop;
this._opening = false;

this.open();
Expand Down Expand Up @@ -152,12 +150,23 @@ File.prototype.log = function (info, callback) {
self._endStream(function () { self._nextFile(); });
}

var written = this._stream.write(output, logged);
if (written === false) ++this._drains;
if (!this._drains) callback(); // eslint-disable-line
else this._next = callback;
if (self._drains > 0) {
self._stream.once('drain', function () {
self._drains--;
return self.log(info, callback);
});
return;
}

return written;
var shouldContinue = self._stream.write(output, logged);

if (!shouldContinue) {
self._drains++;
}

callback();

return shouldContinue;
};

//
Expand Down Expand Up @@ -391,26 +400,17 @@ File.prototype._needsNewFile = function (size) {
return this.maxsize && size >= this.maxsize;
};

File.prototype._onDrain = function onDrain() {
if (--this._drains) return;
var next = this._next;
this._next = noop;
next();
};

File.prototype._onError = function onError(err) {
this.emit('error', err);
};

File.prototype._setupStream = function (stream) {
stream.on('error', this._onError);
stream.on('drain', this._onDrain);
return stream;
};

File.prototype._cleanupStream = function (stream) {
stream.removeListener('error', this._onError);
stream.removeListener('drain', this._onDrain);
return stream;
};

Expand Down Expand Up @@ -448,7 +448,6 @@ File.prototype._createStream = function () {
// TODO: what should we do with errors here?
debug(err);
})
.on('drain', this._onDrain)
.on('open', function () {
debug('file open ok', fullpath);
self.emit('open');
Expand Down

0 comments on commit d43919f

Please sign in to comment.