TypeError: winston.Logger is not a constructor with winston and morgan

TypeError: winston.Logger is not a constructor with winston and morgan

我尝试用 Winston 代替 logger。我在一个项目中使用了他们,当我将代码从他们复制粘贴到当前现有项目时,它运行良好,而不是我遇到 TypeError: winston.Logger is not a constructor

这样的问题

let logger = new (winston.Logger)({ ^

TypeError: winston.Logger is not a constructor

请指导我,为什么会出现这个错误,我应该怎么做才能解决这个问题。

"morgan": "^1.9.0", "winston": "^3.0.0"

以下是我在 logger.js 文件中的代码。

var appRoot = require('app-root-path');
var winston = require('winston');

var options = {
  file: {
    level: 'info',
    name: 'file.info',
    filename: `${appRoot}/logs/app.log`,
    handleExceptions: true,
    json: true,
    maxsize: 5242880, // 5MB
    maxFiles: 100,
    colorize: true,
  },
  errorFile: {
    level: 'error',
    name: 'file.error',
    filename: `${appRoot}/logs/error.log`,
    handleExceptions: true,
    json: true,
    maxsize: 5242880, // 5MB
    maxFiles: 100,
    colorize: true,
  },
  console: {
    level: 'debug',
    handleExceptions: true,
    json: false,
    colorize: true,
  },
};


// your centralized logger object
let logger = new (winston.Logger)({
  transports: [
    new (winston.transports.Console)(options.console),
    new (winston.transports.File)(options.errorFile),
    new (winston.transports.File)(options.file)
  ],
  exitOnError: false, // do not exit on handled exceptions
});

正如你所说,你正在使用3.0.0,你不能不使用winston.Logger,你可以参考库代码(https://github.com/winstonjs/winston/blob/master/lib/winston.js#L178

您需要对代码进行小幅更新,使用 winston.createLogger 而不是 new (winston.Logger)

// your centralized logger object
let logger = winston.createLogger({
  transports: [
    new (winston.transports.Console)(options.console),
    new (winston.transports.File)(options.errorFile),
    new (winston.transports.File)(options.file)
  ],
  exitOnError: false, // do not exit on handled exceptions
});