Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Log Full Stack Trace with Winston 3?

My logger is set up like:

const myFormat = printf(info => {    return `${info.timestamp}: ${info.level}: ${info.message}: ${info.err}`;  });    const logger =    winston.createLogger({    level: "info",    format: combine(timestamp(), myFormat),     transports: [      new winston.transports.File({      filename:       "./logger/error.log",         level: "error"     }),      new winston.transports.File({        filename:        "./logger/info.log",        level: "info"    })   ] }) 

Then I am logging out some error like this:

logger.error(`GET on /history`, { err }); 

How is it possible to log the full stack trace for errors to via the error transport? I tried passing in the err.stack and it came out as undefined.

Thanks !

like image 888
Anthony Xie Avatar asked Nov 10 '17 21:11

Anthony Xie


People also ask

How do I change the log level in Winston?

You can change the logging level in runtime by modifying the level property of the appropriate transport: var log = new (winston. Logger)({ transports: [ new (winston. transports.

How do I view Winston logs?

Go to the example. log file in the logs folder to view the log. Winston allows you to implement multiple logging transports, i.e., a log can be recorded to a file, console, or database. The Logger configuration below logs to a console and a file.

What is Winston logs?

winston is designed to be a simple and universal logging library with support for multiple transports. A transport is essentially a storage device for your logs. Each winston logger can have multiple transports (see: Transports) configured at different levels (see: Logging levels).

How do you print Stacktrace in logs?

To print a stack trace to log you Should declare logger and method info(e. toString()) or log(Level.INFO, e. toString()). Logging is the process of writing log messages during the execution of a program to get error and warning messages as well as info messages.


1 Answers

For winston version 3.2.0+, following will add stacktrace to log output:

import { createLogger, format, transports } from 'winston';  const { combine, timestamp, prettyPrint, colorize, errors,  } = format;   const logger = createLogger({   format: combine(     errors({ stack: true }), // <-- use errors format     colorize(),     timestamp(),     prettyPrint()   ),   transports: [new transports.Console()], });   

Ref: https://github.com/winstonjs/winston/issues/1338#issuecomment-482784056

like image 164
Murli Prajapati Avatar answered Sep 19 '22 20:09

Murli Prajapati