问题
I am applying logger in node js application using Winston but getting winston.Logger is not a constructor. I am following the link below
http://thisdavej.com/using-winston-a-versatile-logging-library-for-node-js/
回答1:
Try something like below code, it should work (it might have happened since you have been using an older code for the newer version):
const winston = require('winston');
require('winston-daily-rotate-file');
function getLogger(module) {
const transport = new winston.transports.DailyRotateFile({
filename: './logs/log',
datePattern: 'yyyy-MM-dd.',
prepend: true,
level: process.env.ENV === 'development' ? 'silly' : 'error',
});
// const logger = new (winston.Logger)({
// transports: [
// transport,
// ],
// });
const logger = winston.createLogger({
transports: [transport],
});
return logger;
}
If you see the commented code, the new
is what gives an error of not being a constructor
回答2:
You are getting this error because winston.Logger
has been changed to winston.createLogger
. Updating your code to use .createLogger
should fix your problem.
Refer to this documentation https://github.com/winstonjs/winston#usage
来源:https://stackoverflow.com/questions/51096367/node-js-logging-winston-logger-is-not-a-constructor