winston-logzio icon indicating copy to clipboard operation
winston-logzio copied to clipboard

exitOnError / .handleExceptions()

Open mrsufgi opened this issue 7 years ago • 4 comments

Hey,

I've tried adding the exitOnError and handleExceptions to my logger but they don't work.

I'm using different containers and I want one container to exitOnError and handleExcepetions because critical errors are not being sent (because the app shutdown too early..)

thanks :)

mrsufgi avatar Jul 12 '16 12:07 mrsufgi

+1

albertdb avatar May 31 '18 08:05 albertdb

Hi, using: "winston": "^2.4.1", "winston-logzio": "^1.0.8"

i have tried this code:

const winston = require('winston');
const winstonLogzIO = require('winston-logzio');

const loggerOptions = {
    token: 'ekByhvPZHSsGvYOGfAkrEpHIiFvybwzH',
    type: 'bla',     // OPTIONAL (If none is set, it will be 'nodejs')
    debug: true,
};


const logzIOTransport = new (winstonLogzIO)(loggerOptions);
const logger = new (winston.Logger)({
    transports: [
        logzIOTransport
    ],
    exceptionHandlers: [
        logzIOTransport
    ],
    exitOnError: true    // set this to true
});

process.on('uncaughtException', function (err) {
    console.error("UncaughtException processing: %s", err);
    logger.error("UncaughtException processing: %s", err);
    logzIOTransport.flush(function (callback) {
        process.exit(1);
    });
});

winston.log('info', { 'wwp': 'asd' });

setTimeout(() => procces.exit(), 3000);

throw new Error('test error');

and logs were sent successfuly

info:  wwp=asd
UncaughtException processing: Error: test error
logzio-nodejs: Sending last messages and closing...
logzio-nodejs: Sending bulk #1
UncaughtException processing: ReferenceError: procces is not defined
logzio-nodejs: Sending last messages and closing...
logzio-nodejs: Sending bulk #2

can you elaborate, or share a code snippet with the issue?

AlonMiz avatar Jun 03 '18 06:06 AlonMiz

Process.on statement wouldn't be necessary if handleExceptions worked properly. In other words, that is the "patchy" way of handling exceptions, specially when having to flush the transport.

albertdb avatar Jun 03 '18 09:06 albertdb

indeed, we would love a PR for that to be submitted 👍

AlonMiz avatar Jun 18 '18 05:06 AlonMiz