【发布时间】:2020-05-18 05:38:15
【问题描述】:
我使用 winston 和 winston-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