Winston: How to handle uncaught exceptions

Created on 2 Dec 2011  路  5Comments  路  Source: winstonjs/winston

I want to log the uncaught exceptions with Winston and exit process. I've configured logger like this

var winston = require('winston');
var logger = new (winston.Logger)({
    transports: [
      new winston.transports.File({ filename: './all-logs.log', timestamp: true, maxsize: 1000000 })
    ],
    exceptionHandlers: [
      new winston.transports.File({ filename: './exceptions.log', timestamp: true, maxsize: 1000000 })
    ],  
    exitOnError: true,
});

Then try throw one error, but no exceptions.logs file was created
throw new Exception("Error here ...");

Did I have the right configuration?

Most helpful comment

@gowthaman-i2i this is the winston based example:

var winston = require('winston');
var logger = new (winston.Logger)({
    transports: [
      new winston.transports.File({ filename: './all-logs.log', timestamp: true, maxsize: 1000000 })
    ],
    exceptionHandlers: [
      new winston.transports.File({ filename: './exceptions.log', timestamp: true, maxsize: 1000000 })
    ],  
    exitOnError: false, // <--- set this to false
});

but @3rd-Eden is correct this can be handled in multiple places :+1:

All 5 comments

I also ran into this problem, and was successful with the following example:

var winston = require('winston');

var logger = new (winston.Logger)({
    transports: [
        new winston.transports.Console(
            {
                level: 'debug',
                colorize: true,
                timestamp: true
            }),
        new winston.transports.File(
            {
                level: 'info',
                colorize: false,
                timestamp: true,
                json: true,
                filename: '/var/log/mylog.log',
                handleExceptions: true
            })
    ]
});

x();

@kenperkins is correct, but this is fixed in HEAD will go out in the next day or two.

if i get uncaughtException application going to exit.
i want to handle uncaughtException without exit appliaction.

@gowthaman-i2i this is the winston based example:

var winston = require('winston');
var logger = new (winston.Logger)({
    transports: [
      new winston.transports.File({ filename: './all-logs.log', timestamp: true, maxsize: 1000000 })
    ],
    exceptionHandlers: [
      new winston.transports.File({ filename: './exceptions.log', timestamp: true, maxsize: 1000000 })
    ],  
    exitOnError: false, // <--- set this to false
});

but @3rd-Eden is correct this can be handled in multiple places :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alditis picture alditis  路  3Comments

pocesar picture pocesar  路  3Comments

JaehyunLee-B2LiNK picture JaehyunLee-B2LiNK  路  3Comments

Tonacatecuhtli picture Tonacatecuhtli  路  4Comments

exortech picture exortech  路  3Comments