TypeError:winston.Logger不是winston和morgan的構造函數

[英]TypeError: winston.Logger is not a constructor with winston and morgan


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

我嘗試使用Winston進行記錄。我在一個項目中使用它當我將代碼從它們粘貼到當前現有項目時比我遇到類似TypeError這樣的問題時運行良好:winston.Logger不是構造函數

let logger = new (winston.Logger)({ ^

讓logger = new(winston.Logger)({^

TypeError: winston.Logger is not a constructor

TypeError: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”,“winston”:“^ 3.0.0”

Following is my code in logger.js file.

以下是我在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
});

1 个解决方案

#1


15  

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.createLogger instead 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
});

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/06/28/1ceab1b25ac4200803c13ee7ec3e0175.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com