首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >温斯顿-elasticsearch在日志记录时创建回调错误

温斯顿-elasticsearch在日志记录时创建回调错误
EN

Stack Overflow用户
提问于 2018-10-15 18:55:16
回答 1查看 2.7K关注 0票数 2

在使用“温斯顿-elasticsearch”时,我在记录消息时会收到此错误:

代码语言:javascript
复制
TypeError: callback is not a function

我的代码:

代码语言:javascript
复制
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")

我看到的是:

代码语言:javascript
复制
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

我使用节点@8.9、温斯顿@2.4.1和温斯顿-elasticsearch@0.7.0。ELASTIC_SEARCH_LOGGING_URL env变量是精确的。

错误发生在库中:

代码语言:javascript
复制
  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();
  }

它调用未定义的‘回调()’。

我是不是错配置了?是否有更好的方法通过温斯顿将应用程序日志发送到ES?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-17 02:42:32

需要温斯顿3.0或更高版本。

代码语言:javascript
复制
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"
  }));
}

温斯顿-elasticsearch开发人员的解决方案:https://github.com/vanthome/winston-elasticsearch/issues/69#issuecomment-430124467

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52823107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档