如何使用Winston 3记录完整堆栈跟踪? [英] How to Log Full Stack Trace with Winston 3?

查看:113
本文介绍了如何使用Winston 3记录完整堆栈跟踪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的记录器设置如下:

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 });

如何通过错误传输记录错误的完整堆栈跟踪?我尝试传入err.stack,它出现了未定义。

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.

谢谢!

推荐答案

你可以写一个格式化程序来传递 error.stack 来记录。

You can write a formatter to pass error.stack to log.

const errorStackFormat = winston.format(info => {
  if (info instanceof Error) {
    return Object.assign({}, info, {
      stack: info.stack,
      message: info.message
    })
  }
  return info
})

const logger = winston.createLogger({
  transports: [ ... ],
  format: winston.format.combine(errorStackFormat(), myFormat)
})

logger.info(new Error('yo')) // => {message: 'yo', stack: "Error blut at xxx.js:xx ......"} 

(输出将取决于您的配置)

(the output will depend on your configuration)

这篇关于如何使用Winston 3记录完整堆栈跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆