node.js 类型错误:winston.Logger 不是带有 winston 和 morgan 的构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51074805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: winston.Logger is not a constructor with winston and morgan
提问by Gunjan Patel
I tried with Winstonfor 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
我试过Winstonfor logger。我在一个项目中使用了它们,当我将代码从它们复制粘贴到当前现有项目时,它运行良好,而不是遇到类似的问题TypeError: winston.Logger is not a constructor
let logger = new (winston.Logger)({ ^
TypeError: winston.Logger is not a constructor
让 logger = new (winston.Logger)({ ^
类型错误:winston.Logger 不是构造函数
Please guide me, Why this error and what should I have to do for solving this issue.
请指导我,为什么会出现这个错误以及我应该怎么做才能解决这个问题。
"morgan": "^1.9.0", "winston": "^3.0.0"
“摩根”:“^1.9.0”,“温斯顿”:“^3.0.0”
Following is my code in logger.jsfile.
以下是我在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
});
回答by Arif Khan
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)
正如你提到的,你正在使用3.0.0,你不能不使用winston.Logger,你可以参考库代码(https://github.com/winstonjs/winston/blob/master/lib/winston.js#L178)
You need to make small update in your code, use winston.createLoggerinstead of new (winston.Logger)
您需要在代码中进行小的更新,使用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
});

