【问题标题】:Node logging with Winstonjs logger gives TypeError使用 Winstonjs 记录器进行节点记录会产生 TypeError
【发布时间】:2018-04-07 12:37:30
【问题描述】:

我刚开始使用 Node,现在我想在我的应用程序中添加一些日志记录,Winstonjs 似乎非常适合。所以我先安装了它:

npm install winston

然后我从自述文件中复制了第一个示例代码(并在其前面添加了要求):

"use strict";

let winston = require('winston');

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  transports: [
    //
    // - Write to all logs with level `info` and below to `combined.log` 
    // - Write all logs error (and below) to `error.log`.
    //
    new winston.transports.File({ filename: 'error.log', level: 'error' }),
    new winston.transports.File({ filename: 'combined.log' })
  ]
});

//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
// 
if (process.env.NODE_ENV !== 'production') {
  logger.add(new winston.transports.Console({
    format: winston.format.simple()
  }));
}

但我得到一个错误:

/Users/kramer65/mms/testlogging.js:7
    format: winston.format.json(),
                          ^
TypeError: Cannot read property 'json' of undefined

有人知道我在这里做错了什么吗?欢迎所有提示!

【问题讨论】:

  • 检查安装了npm ls winston的版本,这似乎是v3代码。
  • @T.J.Crowder - 我试过了,但在我的办公椅周围走动也没什么区别.. ;-) 。关于主题:感谢您复制问题。我刚刚浏览了源代码并找到了这个文件夹:github.com/winstonjs/winston/tree/master/lib/winston/transports,其中没有 json。我不知道这是否能以某种方式帮助我们?
  • @GabrielBleu - 我安装的 winston 版本是 2.4.0
  • 如果你想使用 v3 安装 npm i winston@next --save doc
  • @GabrielBleu - 你是对的。感谢那!您能否将您的评论添加为答案,以便我接受。

标签: javascript node.js logging winston


【解决方案1】:

您的代码与尚未发布的新 v3 兼容。如果你想安装它:

npm i winston@next --save

或者如果你想坚持使用 v2,你可以阅读npm上的 v2 文档

ref

【讨论】:

    猜你喜欢
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多