【问题标题】:Kibana Not Showing Logs Sent to Elasticsearch From Node.js Winston LoggerKibana 不显示从 Node.js Winston Logger 发送到 Elasticsearch 的日志
【发布时间】:2020-05-18 05:38:15
【问题描述】:

我使用 winstonwinston-elasticsearch 包从 Node.js 应用程序直接向 Elasticsearch 发送日志。使用 Docker Compose 将 Elasticsearch 7.5.1、Logstash 和 Kibana 7.5.1 部署在远程服务器上。

问题一: 运行发送2条日志消息给Elasticsearch的node.js文件后,程序没有自动退出返回终端。在 Mac OS X Mojave 10.14.6 上使用 Node.js v12.6.0。

问题 2: 将这 2 条日志消息发送到 Elasticsearch 后,可以使用 Web 浏览器 http://<example.com>:9200/logs-2020.02.01/_search 查看它们。

{"took":5,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"logs-2020.02.01","_type":"_doc","_id":"85GgA3ABiaPPk4as1pEc","_score":1.0,"_source":{"@timestamp":"2020-02-02T02:00:35.789Z","message":"a debug message","severity":"debug","fields":{}}},{"_index":"logs-2020.02.01","_type":"_doc","_id":"9JGgA3ABiaPPk4as1pEc","_score":1.0,"_source":{"@timestamp":"2020-02-02T02:00:35.791Z","message":"an info log","severity":"info","fields":{}}}]}}

但是,这些日志不会显示在 Kibana 上,例如 https://<example.com>/app/infra#/logs/stream?_g=() 的日志部分。

知道如何让日志也显示在 Kibana 上吗?另外,为什么发送日志消息后Node.js应用程序没有退出?

谢谢!

Node.js 应用程序

const winston = require('winston');
const ElasticsearchWinston = require('winston-elasticsearch');
const options = {
    console: {
        level: 'debug',
        handleExceptions: true,
        json: false,
        colorize: true
    },
    elasticsearch: {
        level: 'debug',
        clientOpts: {
            node: 'http://user:pass@example.com:9200',
            log: 'debug',
            maxRetries: 2,
            requestTimeout: 10000,
            sniffOnStart: false,
        }
    }
}

var logger = winston.createLogger({
    exitOnError: false,
    transports: [
        new winston.transports.Console(options.console),
        new ElasticsearchWinston(options.elasticsearch)
    ]
});

logger.debug('a debug message');
logger.info('an info log');

【问题讨论】:

  • 不确定您是否对此进行了排序。但是为了让日志显示在日志流中,您需要使用带有创建操作的批量 API。和logs-yy-MM-dd 的索引,所以logs-2022-01-07@timestamp 是必需的,至少肯定是message - 我认为日志流是ES 7.9 中的一个新概念。我们为此使用 Bulk API,并且我们的日志显示在此流中。只是为了好奇 - Stream 概念来自 EventSourcing Streams。

标签: javascript node.js elasticsearch kibana elk


【解决方案1】:

我不是 node.js 专家,所以我只会关注 kibana 问题。日志应用程序不适用于像您这样的“自定义”日志/索引。

如文档中所述 (https://www.elastic.co/guide/en/kibana/current/xpack-logs.html):

Kibana 中的 Logs 应用可让您浏览常用服务器、容器和服务的日志。

日志应用程序用于监控您的基础设施和 ELK 服务,例如通过某些 Beats 模块(例如 Filebeat 的 Elasticsearch、Kibana 和 Logstash 模块)。

也来自文档 (https://www.elastic.co/guide/en/kibana/current/xpack-logs-configuring.html):

日志的默认源配置在 Kibana 配置文件的日志应用设置中指定。默认配置使用 filebeat-* 索引模式来查询数据。

这解释了为什么您在日志应用程序中看不到任何数据,因为您的索引使用“logs-*”索引模式。

长话短说:

要查看 log-* 索引中的文档,您需要打开 Discovery(Kibana 左侧边栏上的第一个图标)并选择您已经设置的索引模式。这是在 Kibana 中搜索应用程序数据的适当方式。

希望能帮到你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 2016-04-18
    • 2020-04-07
    • 2015-04-15
    • 2020-10-08
    • 2020-09-26
    • 1970-01-01
    相关资源
    最近更新 更多