-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
winstonversion?-
winston@2 -
winston@3
-
node -voutputs: v10.16.0- Operating System? macOS
- Language? ES6/7
What is the problem?
In both below cases nodejs will print default unhandled rejection error
- setting
handleRejectionsflag totrueduring transport creation is not working - calling logger.rejections.handle(transport) is not working
What do you expect to happen instead?
Winston should intercept unhandled rejection and log it
Other information
1st problem is due to not released change in winston-transport package: winstonjs/winston-transport#47
2nd problem is due to typo in _addHandler method of lib/winston/rejection-handler.js:
winston/lib/winston/rejection-handler.js
Line 153 in 15c9653
| handler.handleExceptions = true; |
This line should be ADDED handler.handleRejections = true
There is one important thing: without handleExceptions, handleRejections won't log anything, due to below check: https://github.com/winstonjs/winston-transport/blob/46db8f3c8cd8b106ade8d7e04a191ee388683d60/index.js#L70
Testcases
Fails
const { createLogger, transports } = require('winston');
const logger = createLogger({
transports: new transports.Console({
level: 'info',
handleRejections: true,
}),
});
logger.info('Start');
new Promise((resolve, reject) => {
process.nextTick(() => {
reject(new Error('Rejected'));
});
}).then(() => {});
Succeeds (Trick is in using Object.assign on transport instance)
const { createLogger, transports } = require('winston');
const logger = createLogger({
transports: Object.assign(
new transports.Console({
handleExceptions: true,
}),
{
handleRejections: true,
},
),
});
logger.info('Start');
new Promise((resolve, reject) => {
process.nextTick(() => {
reject(new Error('Rejected'));
});
}).then(() => {});