使用Winstonjs记录器进行节点记录会产生TypeError

时间:2017-10-26 08:15:40

标签: javascript node.js logging winston

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

npm install winston

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

"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

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

1 个答案:

答案 0 :(得分:3)

您的代码与尚未发布的新v3兼容。如果您要安装它:npm i winston@next --save

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

ref

相关问题