如何修复错误:ConfigurationError: Missing node(s) option for winston-elasticsearch?

ste*_*ien 5 cucumber elasticsearch winston

我想在 Cucumber 中使用带有 ElectricSearch 的日志我创建了一个类 Logger

var winston = require('winston');
var Elasticsearch = require('winston-elasticsearch');
  var instance
  var logger
  var esTransportOpts = {
  level: 'info' 
   }

class Logger { 

    constructor() {
        logger = winston.createLogger({
           transports: [
               new Elasticsearch(esTransportOpts)
          ]
       })
    }

     static getLogger() { 
     if (!instance)  { 
        instance = new Logger()
      }
     return logger
   }


   createLogger () { 
        let logger = winston.createLogger({
           level: 'info',
           format: winston.format.json(),
            defaultMeta: { service: 'user-service' },
            transports: [

                   new winston.transports.File({ filename: 'combined.log' })
            ]
        });

         //
         // If we're not in production then log to the `console` with the format:
         // `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
         // 
         if (process.env.NODE_ENV !== 'production') {
           logger.add(new winston.transports.Console({
            format: winston.format.simple()
        }));
    }

   }


    static addLogging () { 
        winston.add(winston.transports.Logstash, {
          port: 28777,
          node_name: 'my node name',
          host: '127.0.0.1'
        });

    }
}
module.exports = Logger
Run Code Online (Sandbox Code Playgroud)

在 Cucumber 的 Before 语句中,我创建了一个记录器

const { Before, Given, When, Then } = require('cucumber')
const Logger = require('../../src/Logger')

        let a,b, r , logger


         Before(function() {
             logger = Logger.getLogger()
          })

         Given('I have first variable {int}', function (int) {
           logger.info("Multipler")
           a = int

         });



         Given('I have second variable {int}', function (int) {
           b = int

         });

         When('Multiplication a and b', function() {
           r = a*b
         })



         Then('I display the Result  {int}', function (int) {
             int = r
             logger.info(a, "multiplyes with", b, "is", r )
             return int
         });
Run Code Online (Sandbox Code Playgroud)

当我执行这个黄瓜测试时,我收到错误消息:

黄瓜2@0.1.0 黄瓜 /Users/steinkorsveien/Development/Cucumber/cucumber2 黄瓜-js "systemtest/Multiplier.feature" F----

失败:

1) 场景:a 和 b 相乘 # systemtest/Multiplier.feature:24
? 在 # systemtest/step-definition/multiplier.js:7 ConfigurationError: Missing node(s) option at new Client (/Users/steinkorsveien/Development/Cucumber/cucumber2/node_modules/@elastic>/elasticsearch/index.js:52: 之前: 13) 在新的 Elasticsearch (/Users/steinkorsveien/Development/Cucumber/cucumber2/node_modules/winston->elasticsearch/index.js:57:21) 在新的 Logger >(/Users/steinkorsveien/Development/Cucumber/cucumber2/src/ Logger.js:13:16) 在 Function.getLogger >(/Users/steinkorsveien/Development/Cucumber/cucumber2/src/Logger.js:20:20) 在 World。>(/Users/steinkorsveien/Development/Cucumber/cucumber2/systemtest/step->definition/multiplier.js:8:30)

BaD*_*mer 4

正如这里提到的

您缺少 elasticsearch 客户端配置。

var esTransportOpts = {
   level: 'info',
   clientOpts: { node: 'http://localhost:9200' }
}
Run Code Online (Sandbox Code Playgroud)

这样你就告诉温斯顿把日志转储到哪里。