Search code examples
node.jsapache-kafkawinston

winston3-kafka library used for sending logs from node app to kafka not working


I am trying to send my Winston logs messages to Kafka, using winston transport. I found this library to help me out since my Winston version is 3.X so many of the 2.X versions do not work.

https://github.com/aidtechnology/winston3-kafka

Here is the example I am trying to use.

var winston = require('winston');
winston.transports.Kafka = require('winston3-kafka');


var options = {
  topic: 'logs',
  clientOptions: {
  kafkaHost: {'localhost:9092'}  // We connect directly to Kafka, rather than Zookeeper
  }
};

winston.add(new winston.transports.Kafka(options));

The following error I get is.

SyntaxError: Unexpected token }
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

Not sure why this syntax error is occurring. I am copying and pasting from the usage.


Solution

  • You need to use:

    kafkaHost: 'localhost:9092'

    instead of:

    kafkaHost: {'localhost:9092'}

    So, yes, you have a syntax error.

    var winston = require('winston');
    winston.transports.Kafka = require('winston3-kafka');
    
    
    var options = {
      topic: 'logs',
      clientOptions: {
      kafkaHost: 'localhost:9092'  // We connect directly to Kafka, rather than Zookeeper
      }
    };
    

    ;)