Search code examples
node.jselasticsearchwinston

winston-elasticsearch creates callback error when logging


When using 'winston-elasticsearch' I am getting this error when logging a message:

TypeError: callback is not a function

My code:

const winston = require("winston");
const logger = new winston.Logger();
...
if( process.env.ELASTIC_SEARCH_LOGGING_URL ){

  var elasticsearch = require('elasticsearch');
  var client = new elasticsearch.Client({
    host: process.env.ELASTIC_SEARCH_LOGGING_URL,
    log: 'info'
  }); 

  logger.add( 
    require('winston-elasticsearch'),
    {
      client
    }        
  );
}
//this causes the error
logger.info("hi")

I am seeing this:

clock_1 | TypeError: callback is not a function
clock_1 | at Elasticsearch.log (/usr/app/node_modules/winston-elasticsearch/index.js:105:5)
clock_1 | at transportLog (/usr/app/node_modules/winston/lib/winston/logger.js:234:15)
clock_1 | at /usr/app/node_modules/winston/node_modules/async/lib/async.js:157:13

I use node@8.9,winston@2.4.1 and winston-elasticsearch@0.7.0. The ELASTIC_SEARCH_LOGGING_URL env variable is accurate.

The error occurs here in the library:

  log(info, callback) {
    const level = info[LEVEL];
    const { message } = info;
    let meta = info[SPLAT];
    if (meta !== undefined) {
      // eslint-disable-next-line prefer-destructuring
      meta = meta[0];
    }

    setImmediate(() => {
      this.emit('logged', level);
    });

    const logData = {
      message,
      level,
      meta,
      // timestamp: this.opts.timestamp()
    };
    const entry = this.opts.transformer(logData);
    this.bulkWriter.append(
      this.getIndexName(this.opts),
      this.opts.messageType,
      entry
    );

    callback();
  }

It's invoking 'callback()' which is not defined.

Am I misconfiguring? Are there better ways to send application logs to ES via Winston?


Solution

  • Needs winston version 3.0 or higher.

    const winston = require("winston"); //"winston": "~3",
    const logger = winston.createLogger();
    
    if( process.env.ELASTIC_SEARCH_LOGGING_URL ){
    
      var elasticsearch = require('elasticsearch');
      var winston_elasticsearch = require('winston-elasticsearch');
    
      var client = new elasticsearch.Client({
        host: process.env.ELASTIC_SEARCH_LOGGING_URL,
        log: 'info'
    
      });
    
      logger.add( new winston_elasticsearch({
        client,
        index:"logging"
      }));
    }
    

    Solution from the winston-elasticsearch developers: https://github.com/vanthome/winston-elasticsearch/issues/69#issuecomment-430124467