Search code examples
javascriptnode.jsloggingerror-handlingwinston

How to log Unhandled TimeoutError in Winstonjs logger?


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?


Solution

  • The process is referring to node. Try to add a unhandledRejection check in your index.js or use proper winston exception handling:

    "use strict";
    
    const winston = require('winston');
    
    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,
        // handleExceptions: true // Otherwise, you should use this in options.
    });
    
    process.on('unhandledRejection', (reason, promise) => {
        logger.debug(reason);
    });
    
    process.on('uncaughtException', (err) => {
        logger.debug(err);
    });
    

    However, look in your package.json file in the same directory as the index.js file. Look in dependencies section for node-pool and change its version to "node-pool": "^3.4.2" since the problem you describe is most likely fixed in the 3.1.8 release.