【问题标题】:Why winston is not logging http request为什么winston没有记录http请求
【发布时间】:2017-10-04 09:02:01
【问题描述】:

我正在尝试使用 winston 库记录请求并将其保存到文件中。

logger.debug(req.body);

它保存 [object Object] 而不是请求正文。有谁知道我做错了什么?

【问题讨论】:

  • 很可能debug 正在将toString() 应用于对象。也许试试JSON.stringify(req.body)
  • @evolutionxbox 你能回答这个问题让我接受吗

标签: javascript node.js winston


【解决方案1】:

您可以将 morgan 与 winston 结合使用以自动记录所有请求

var logger = new winston.Logger({
transports: [
    new winston.transports.File({
        level: 'info',
        filename: './logs/all-logs.log',
        handleExceptions: true,
        json: true,
        maxsize: 5242880, //5MB
        maxFiles: 5,
        colorize: false
    })
],
   exitOnError: false
}),

logger.stream = {
    write: function(message, encoding){
        logger.info(message);
    }
};

app.use(require("morgan")("combined", { "stream": logger.stream }));

【讨论】:

    【解决方案2】:

    尝试这样做

    logger.debug(req.body.toSource());
    

    toSource() 方法表示对象的源代码。

    【讨论】:

      猜你喜欢
      • 2018-08-02
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 2017-01-16
      • 2017-04-01
      相关资源
      最近更新 更多