Node js logging - winston.Logger is not a constructor

别说谁变了你拦得住时间么 提交于 2019-12-12 10:28:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!