How to Log Full Stack Trace with Winston 3?

Anthony Xie picture Anthony Xie · Nov 10, 2017 · Viewed 21.5k times · Source

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 !

Answer

Murli Prajapati picture Murli Prajapati · Oct 20, 2019

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