Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pino error log is empty, although error object contains information

I have written a small error handling function, which is invoked after an AXIOS request, like so:

try {
 ...
} catch (error) {
    handleAxiosError(error);
}

The function is as follows:

function handleAxiosError(error) {
    if (error.response !== undefined) {
        logger.error(`Received a HTTP error. Status code: ${error.response.status}, Data: ${error.response.data}`);
    } else if (error.request !== undefined) {
        logger.error(error.request);
    } else {
        logger.error(error.message);
    }
    throw new Error(error);
}

Although an error is thrown:

(node:94324) UnhandledPromiseRejectionWarning: Error: Error: connect ECONNREFUSED 127.0.0.1:6557 at handleAxiosError (C:\pathtoapp\utils\utils.js:66:11)

Pino only saves the following to the log. I can't find the problem. Is this an async issue?

{"level":50,"time":1567435455281,"pid":94324,"hostname":"host","name":"app","res":{},"v":1}

Thanks!

like image 923
rStorms Avatar asked Nov 07 '22 14:11

rStorms


1 Answers

When using async logging (the default for the Pino logger), the process might have exited before all of the logging has been processed.

See https://github.com/pinojs/pino/blob/HEAD/docs/asynchronous.md

You can also change the logging to be synchronous, and you won't have this problem:

const dest = pino.destination({ sync: true })

like image 128
Francis Upton IV Avatar answered Nov 14 '22 23:11

Francis Upton IV