我尝试Winston
用logger
。我在一个项目中使用了他们的项目,当我将代码从他们复制粘贴到当前现有项目中时,效果很好,而不是遇到TypeError: winston.Logger is not a constructor
let logger = new(winston.Logger)({ ^
TypeError:winston.Logger不是构造函数
请指导我,为什么会出现此错误,以及解决该问题应该怎么做。
“摩根”:“ ^ 1.9.0”,“温斯顿”:“ ^ 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
});
答案 0 :(得分:56)
如前所述,您正在使用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
});