-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add handleRejections support (#1462)
* Add handleRejections suppot * Correct typo
- Loading branch information
Showing
9 changed files
with
653 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ const asyncForEach = require('async/forEach'); | |
const { LEVEL, SPLAT } = require('triple-beam'); | ||
const isStream = require('is-stream'); | ||
const ExceptionHandler = require('./exception-handler'); | ||
const RejectionHandler = require('./rejection-handler'); | ||
const LegacyTransportStream = require('winston-transport/legacy'); | ||
const Profiler = require('./profiler'); | ||
const { warn } = require('./common'); | ||
|
@@ -88,7 +89,8 @@ class Logger extends Transform { | |
padLevels, | ||
rewriters, | ||
stripColors, | ||
exceptionHandlers | ||
exceptionHandlers, | ||
rejectionHandlers | ||
} = {}) { | ||
// Reset transports if we already have them | ||
if (this.transports.length) { | ||
|
@@ -103,6 +105,7 @@ class Logger extends Transform { | |
this.levels = levels || this.levels || config.npm.levels; | ||
this.level = level; | ||
this.exceptions = new ExceptionHandler(this); | ||
this.rejections = new RejectionHandler(this); | ||
this.profilers = {}; | ||
this.exitOnError = exitOnError; | ||
|
||
|
@@ -113,19 +116,28 @@ class Logger extends Transform { | |
} | ||
|
||
if ( | ||
colors || emitErrs || formatters || | ||
padLevels || rewriters || stripColors | ||
colors || | ||
emitErrs || | ||
formatters || | ||
padLevels || | ||
rewriters || | ||
stripColors | ||
) { | ||
throw new Error([ | ||
'{ colors, emitErrs, formatters, padLevels, rewriters, stripColors } were removed in [email protected].', | ||
'Use a custom winston.format(function) instead.', | ||
'See: https://github.com/winstonjs/winston/tree/master/UPGRADE-3.0.md' | ||
].join('\n')); | ||
throw new Error( | ||
[ | ||
'{ colors, emitErrs, formatters, padLevels, rewriters, stripColors } were removed in [email protected].', | ||
'Use a custom winston.format(function) instead.', | ||
'See: https://github.com/winstonjs/winston/tree/master/UPGRADE-3.0.md' | ||
].join('\n') | ||
); | ||
} | ||
|
||
if (exceptionHandlers) { | ||
this.exceptions.handle(exceptionHandlers); | ||
} | ||
if (rejectionHandlers) { | ||
this.rejections.handle(rejectionHandlers); | ||
} | ||
} | ||
|
||
isLevelEnabled(level) { | ||
|
@@ -183,7 +195,8 @@ class Logger extends Transform { | |
* | ||
*/ | ||
/* eslint-enable valid-jsdoc */ | ||
log(level, msg, ...splat) { // eslint-disable-line max-params | ||
log(level, msg, ...splat) { | ||
// eslint-disable-line max-params | ||
// Optimize for the hotpath of logging JSON literals | ||
if (arguments.length === 1) { | ||
// Yo dawg, I heard you like levels ... seriously ... | ||
|
@@ -276,7 +289,10 @@ class Logger extends Transform { | |
// Remark: not sure if we should simply error here. | ||
if (!this._readableState.pipes) { | ||
// eslint-disable-next-line no-console | ||
console.error('[winston] Attempt to write logs with no transports %j', info); | ||
console.error( | ||
'[winston] Attempt to write logs with no transports %j', | ||
info | ||
); | ||
} | ||
|
||
// Here we write to the `format` pipe-chain, which on `readable` above will | ||
|
@@ -300,11 +316,15 @@ class Logger extends Transform { | |
*/ | ||
_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); | ||
asyncForEach( | ||
transports, | ||
(transport, next) => { | ||
if (!transport || transport.finished) return setImmediate(next); | ||
transport.once('finish', next); | ||
transport.end(); | ||
}, | ||
callback | ||
); | ||
} | ||
|
||
/** | ||
|
@@ -318,12 +338,15 @@ class Logger extends Transform { | |
// 1. They inherit from winston.Transport in < 3.x.x which is NOT a stream. | ||
// 2. They expose a log method which has a length greater than 2 (i.e. more then | ||
// just `log(info, callback)`. | ||
const target = !isStream(transport) || transport.log.length > 2 | ||
? new LegacyTransportStream({ transport }) | ||
: transport; | ||
const target = | ||
!isStream(transport) || transport.log.length > 2 | ||
? new LegacyTransportStream({ transport }) | ||
: transport; | ||
|
||
if (!target._writableState || !target._writableState.objectMode) { | ||
throw new Error('Transports must WritableStreams in objectMode. Set { objectMode: true }.'); | ||
throw new Error( | ||
'Transports must WritableStreams in objectMode. Set { objectMode: true }.' | ||
); | ||
} | ||
|
||
// Listen for the `error` event and the `warn` event on the new Transport. | ||
|
@@ -335,6 +358,10 @@ class Logger extends Transform { | |
this.exceptions.handle(); | ||
} | ||
|
||
if (transport.handleRejections) { | ||
this.rejections.handle(); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
|
@@ -346,11 +373,14 @@ class Logger extends Transform { | |
remove(transport) { | ||
let target = transport; | ||
if (!isStream(transport) || transport.log.length > 2) { | ||
target = this.transports | ||
.filter(match => match.transport === transport)[0]; | ||
target = this.transports.filter( | ||
match => match.transport === transport | ||
)[0]; | ||
} | ||
|
||
if (target) { this.unpipe(target); } | ||
if (target) { | ||
this.unpipe(target); | ||
} | ||
return this; | ||
} | ||
|
||
|
@@ -523,7 +553,9 @@ class Logger extends Transform { | |
// Attempt to be kind to users if they are still using older APIs. | ||
if (typeof args[args.length - 2] === 'function') { | ||
// eslint-disable-next-line no-console | ||
console.warn('Callback function no longer supported as of [email protected]'); | ||
console.warn( | ||
'Callback function no longer supported as of [email protected]' | ||
); | ||
args.pop(); | ||
} | ||
|
||
|
@@ -546,7 +578,9 @@ class Logger extends Transform { | |
*/ | ||
handleExceptions(...args) { | ||
// eslint-disable-next-line no-console | ||
console.warn('Deprecated: .handleExceptions() will be removed in winston@4. Use .exceptions.handle()'); | ||
console.warn( | ||
'Deprecated: .handleExceptions() will be removed in winston@4. Use .exceptions.handle()' | ||
); | ||
this.exceptions.handle(...args); | ||
} | ||
|
||
|
@@ -557,7 +591,9 @@ class Logger extends Transform { | |
*/ | ||
unhandleExceptions(...args) { | ||
// eslint-disable-next-line no-console | ||
console.warn('Deprecated: .unhandleExceptions() will be removed in winston@4. Use .exceptions.unhandle()'); | ||
console.warn( | ||
'Deprecated: .unhandleExceptions() will be removed in winston@4. Use .exceptions.unhandle()' | ||
); | ||
this.exceptions.unhandle(...args); | ||
} | ||
|
||
|
@@ -566,11 +602,13 @@ class Logger extends Transform { | |
* @throws {Error} - TODO: add throws description. | ||
*/ | ||
cli() { | ||
throw new Error([ | ||
'Logger.cli() was removed in [email protected]', | ||
'Use a custom winston.formats.cli() instead.', | ||
'See: https://github.com/winstonjs/winston/tree/master/UPGRADE-3.0.md' | ||
].join('\n')); | ||
throw new Error( | ||
[ | ||
'Logger.cli() was removed in [email protected]', | ||
'Use a custom winston.formats.cli() instead.', | ||
'See: https://github.com/winstonjs/winston/tree/master/UPGRADE-3.0.md' | ||
].join('\n') | ||
); | ||
} | ||
|
||
/** | ||
|
Oops, something went wrong.