Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log node errors with winston logger?

In my node application I use the Winstonjs logger. Today my application somehow seemed to freeze, but it failed to log something. I stopped the application and ran it manually which showed me this error

ReferenceError: totalValue is not defined

I clearly made a mistake in my code, but my main problem here is that I couldn't know from the Winston logs.

I pasted my Winston implementation below. I created this so that I can simply use log('The log message');. But this doesn't log any occurring node errors.

Does anybody know how I can get every occurring node error into my Winston logs?

const myFormat = winston.format.printf(info => {
    return `${info.timestamp} ${info.level}: ${info.message}`;
});
const logger = winston.createLogger({
    level: 'info',
    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'}),
    ]
});
function log(message, level='info'){
    if (typeof message === 'object'){
        message = JSON.stringify(message);
    }
    logger[level](message);
}
like image 756
kramer65 Avatar asked Apr 05 '18 18:04

kramer65


People also ask

How do I set up a basic logging example using Winston?

Let’s quickly set up a basic logging example using Winston. Don’t worry, it’s not too much. And we’ll be building on top of this basic example as we go. If you want to follow along, start by installing Winston via npm. Then create the following logging code and save it into a file (of any name). Then all you need to do is run the file with:

What is the best logging library for nodejs?

In this guide we’ll focus on a logging package called Winston, an extremely versatile logging library and the most popular logging solution available for Node.js applications, based on NPM download statistics. Winston’s features include support for multiple storage options and log levels, log queries, and even a built-in profiler.

Is it possible to log uncaughtexception events in Winston?

With winston, it is possible to catch and log uncaughtException events from your process. With your own logger instance you can enable this behavior when it's created or later on in your applications lifecycle:

What is the difference between Winston and console logs?

But Winston gives us more powerful features than the humble console log, such as Streams: Winston uses Node.js streams, which is a performant way of chunking up data processing—for instance, data manipulation on large amounts of data. Streams break tasks down into small, manageable chunks so it’s easier for your Node.js system to handle.


1 Answers

Winston can log exceptions for you. From the docs: Exceptions

With winston, it is possible to catch and log uncaughtException events from your process. With your own logger instance you can enable this behavior when it's created or later on in your applications lifecycle:

const { createLogger, transports } = require('winston');

// Enable exception handling when you create your logger.
const logger = createLogger({
  transports: [
    new transports.File({ filename: 'combined.log' }) 
  ],
  exceptionHandlers: [
    new transports.File({ filename: 'exceptions.log' })
  ]
});

// Or enable it later on by adding a transport or using `.exceptions.handle`
const logger = createLogger({
  transports: [
    new transports.File({ filename: 'combined.log' }) 
  ]
});

// Call exceptions.handle with a transport to handle exceptions
logger.exceptions.handle(
  new transports.File({ filename: 'exceptions.log' })
like image 141
gforce301 Avatar answered Oct 25 '22 09:10

gforce301