How can I fix error: ConfigurationError: Missing node(s) option for winston-elasticsearch?

Viewed 6413

I want to use log with ElectricSearch in Cucumber I create a class 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

in the Before statment of Cucumber I create a logger

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
         });

When I exeute this the cucumber test I get the error message:

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

Failures:

1) Scenario: multiplying a and b # systemtest/Multiplier.feature:24
✖ Before # 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) at new Elasticsearch (/Users/steinkorsveien/Development/Cucumber/cucumber2/node_modules/winston->elasticsearch/index.js:57:21) at new Logger >(/Users/steinkorsveien/Development/Cucumber/cucumber2/src/Logger.js:13:16) at Function.getLogger >(/Users/steinkorsveien/Development/Cucumber/cucumber2/src/Logger.js:20:20) at World. >(/Users/steinkorsveien/Development/Cucumber/cucumber2/systemtest/step->definition/multiplier.js:8:30)

2 Answers

as mentioned here

you're missing elasticsearch client configuration.

var esTransportOpts = {
   level: 'info',
   clientOpts: { node: 'http://localhost:9200' }
}

that way you're telling winston where to dump the logs.

I had the same problem when moved from elasticsearch to @elastic/elasticsearch. I changed in winston.transports.Logstash parameters the property host to node with the http:// prefix.

from:

const client = new elasticsearch.Client({
         host: '${esHost}:${esPort}',
      })

to:

const client = new elasticsearch.Client({
        node: 'http://${esHost}:${esPort}'
      })
Related