#StackBounty: #javascript #node.js #logging #error-handling #winston How to log Unhandled TimeoutError in Winstonjs logger?

Bounty: 250

I’ve got a node application in which I use Winstonjs as a logger. In my logger I’ve got a specific part for logging exceptions like this:

const myFormat = winston.format.printf(info => {
    return `${info.timestamp} ${info.level}: ${info.message}`;
const logger = winston.createLogger({
    level: "debug",
    format: winston.format.combine(winston.format.timestamp(), myFormat),  // winston.format.json(),
    transports: [
        new winston.transports.File({filename: "logs/error.log", level: 'error'}),
        new winston.transports.File({filename: 'logs/combined.log'}),
    exceptionHandlers: [
        new winston.transports.File({ filename: 'logs/exceptions.log' }),
        new winston.transports.File({ filename: 'logs/combined.log' })
    exitOnError: false

So any exceptions should be logged in both logs/combined.log and logs/exceptions.log.

When I run my program I now sometimes get the error below in my STDOUT, which comes from Sequelizejs which I use to write to my MySQL db.

Unhandled rejection TimeoutError: ResourceRequest timed out
    at ResourceRequest._fireTimeout (/root/mmjs/node_modules/generic-pool/lib/ResourceRequest.js:62:17)
    at Timeout.bound (/root/mmjs/node_modules/generic-pool/lib/ResourceRequest.js:8:15)
    at ontimeout (timers.js:424:11)
    at tryOnTimeout (timers.js:288:5)
    at listOnTimeout (timers.js:251:5)
    at Timer.processTimers (timers.js:211:10)

This error only shows up in stdout (when running my program manually), and it doesn’t show up in either the combined.log or the exceptions.log. It could be because this is an error and not an exception. I’m not sure how I can log this correctly though.

(I guess I can wrap my whole code in a try/catch, but I guess there’s a better way.. 🙂 )

Could anybody help me out logging this and similar uncaught errors?

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.