Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 43 additions & 9 deletions lib/winston/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
* @param {!Object} options - TODO: add param description.
* @returns {undefined}
*/
configure({

Check warning on line 82 in lib/winston/logger.js

View workflow job for this annotation

GitHub Actions / Tests (16)

Method 'configure' has a complexity of 19. Maximum allowed is 11

Check warning on line 82 in lib/winston/logger.js

View workflow job for this annotation

GitHub Actions / Tests (16)

Method 'configure' has too many statements (24). Maximum allowed is 15

Check warning on line 82 in lib/winston/logger.js

View workflow job for this annotation

GitHub Actions / Tests (20)

Method 'configure' has a complexity of 19. Maximum allowed is 11

Check warning on line 82 in lib/winston/logger.js

View workflow job for this annotation

GitHub Actions / Tests (20)

Method 'configure' has too many statements (24). Maximum allowed is 15

Check warning on line 82 in lib/winston/logger.js

View workflow job for this annotation

GitHub Actions / Tests (18)

Method 'configure' has a complexity of 19. Maximum allowed is 11

Check warning on line 82 in lib/winston/logger.js

View workflow job for this annotation

GitHub Actions / Tests (18)

Method 'configure' has too many statements (24). Maximum allowed is 15
silent,
format,
defaultMeta,
Expand Down Expand Up @@ -227,7 +227,7 @@
*
*/
/* eslint-enable valid-jsdoc */
log(level, msg, ...splat) {

Check warning on line 230 in lib/winston/logger.js

View workflow job for this annotation

GitHub Actions / Tests (16)

Method 'log' has a complexity of 13. Maximum allowed is 11

Check warning on line 230 in lib/winston/logger.js

View workflow job for this annotation

GitHub Actions / Tests (16)

Method 'log' has too many statements (27). Maximum allowed is 15

Check warning on line 230 in lib/winston/logger.js

View workflow job for this annotation

GitHub Actions / Tests (20)

Method 'log' has a complexity of 13. Maximum allowed is 11

Check warning on line 230 in lib/winston/logger.js

View workflow job for this annotation

GitHub Actions / Tests (20)

Method 'log' has too many statements (27). Maximum allowed is 15

Check warning on line 230 in lib/winston/logger.js

View workflow job for this annotation

GitHub Actions / Tests (18)

Method 'log' has a complexity of 13. Maximum allowed is 11

Check warning on line 230 in lib/winston/logger.js

View workflow job for this annotation

GitHub Actions / Tests (18)

Method 'log' has too many statements (27). Maximum allowed is 15
// eslint-disable-line max-params
// Optimize for the hotpath of logging JSON literals
if (arguments.length === 1) {
Expand Down Expand Up @@ -349,15 +349,49 @@
*/
_final(callback) {
const transports = this.transports.slice();
asyncForEach(
transports,
(transport, next) => {
if (!transport || transport.finished) return setImmediate(next);
transport.once('finish', next);
transport.end();
},
callback
);

// We need to ensure the readable buffer is fully drained before ending
// transports. The pipe from Logger to transports is asynchronous - data
// pushed via _transform() goes to the readable buffer, and the pipe reads
// from it to write to transports. If we call transport.end() while there's
// still data in the readable buffer, the pipe will try to write to an
// ending transport, causing "write after end" errors.
//
// We use an event-driven approach: listen for 'data' events which fire
// as the pipe consumes data from our readable buffer. When the buffer
// empties, we can safely end transports.

const endTransports = () => {
asyncForEach(
transports,
(transport, next) => {
if (!transport || transport.finished) return setImmediate(next);
transport.once('finish', next);
transport.end();
},
callback
);
};

const isBufferEmpty = () => {
return !this._readableState || this._readableState.length === 0;
};

// If buffer is already empty, proceed immediately
if (isBufferEmpty()) {
setImmediate(endTransports);
return;
}

// Listen for data consumption - 'data' events fire as pipe reads from buffer
const onData = () => {
if (isBufferEmpty()) {
this.removeListener('data', onData);
endTransports();
}
};

this.on('data', onData);
}

/**
Expand Down
Loading
Loading