本文介绍了TypeError:winston.Logger不是winston和morgan的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将Winston用于logger.我在一个项目中使用了他们的项目,当我将代码从他们复制粘贴到当前现有项目中时,效果很好,而不是遇到像TypeError: winston.Logger is not a constructor

I tried with Winston for logger. I used in one project their It's working well when I copy paste the code from their to the current existing project than I face an issue like TypeError: winston.Logger is not a constructor

TypeError:winston.Logger不是构造函数

TypeError: winston.Logger is not a constructor

请指导我,为什么会出现此错误,以及解决该问题我该怎么做.

Please guide me, Why this error and what should I have to do for solving this issue.

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

Following is my code in logger.js file.

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 )

As you mention, you are using 3.0.0, you can not not use winston.Logger, you may refer library code( https://github.com/winstonjs/winston/blob/master/lib/winston.js#L178 )

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

You need to make small update in your code, use winston.createLogger instead of 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
});

这篇关于TypeError:winston.Logger不是winston和morgan的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 07:00