我使用winston
和winston-elasticsearch
包将日志从 Node.js 应用程序直接发送到 Elasticsearch 。Elasticsearch 7.5.1、Logstash 和 Kibana 7.5.1 使用 Docker Compose 部署在远程服务器上。
问题一:运行发送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:[email protected]: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');
我不是 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 中搜索应用程序数据的合适方式。
我希望我能帮助你。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句