首页 > 解决方案 > 如何使用 Fluentd 将 json-log 发送到 Elasticsearch

问题描述

我的计算机上安装了 Windows 10,并且在 docker 容器中运行了 Elasticsearch/Kibana。

我正在尝试使用 Fluentd 将我的应用程序生成的日志重定向到 Elasticsearch。这是 td-agent.conf 文件:

<source>
  @type tail
  path C:/Projects/log.json
  pos_file C:/Projects/log.json.pos
  tag *
  format json
  time_key @timestamp
</source>

<match **>
  @type elasticsearch
  logstash_format false
  host localhost
  port 9200
  index_name appname-api-*
  type_name fluentd
  flush_interval 1s
</match>

这是 appsettings 的一部分,我在其中指定了我想要 elasticsearch 格式的 json 文件:

  {
    "Name": "File",
    "Args": {
      "path": "c:/Projects/log.json",
      "formatter": "Serilog.Formatting.Elasticsearch.ElasticsearchJsonFormatter, Serilog.Formatting.Elasticsearch"
    }
  }

这是日志文件中的一行:

{"@timestamp":"2021-10-22T11:13:39.4325643+03:00","level":"Information","messageTemplate":"Now listening on: {address}","message":"Now listening on: \"http://localhost:5001\"","fields":{"address":"http://localhost:5001","SourceContext":"Microsoft.Hosting.Lifetime","MachineName":"MACHINENAME"}}

但它不起作用。我怀疑我的 td-agent.conf。你能给我举个例子吗?

或者也许更容易切换到 Filebeat 或其他东西?

标签: elasticsearchserilogfluentd

解决方案


知道了!这是正确的 td-agent.conf

<system>
  log_level debug
</system>

<source>
  @type tail
  path C:/Projects/log.json
  pos_file C:/Projects/log.json.pos
  tag log_test
  emit_unmatched_lines true

  <parse>
    @type json
  </parse>
</source>

<match log_test>
  @type elasticsearch
  host localhost
  port 9200
  index_name appname-api-2021-10
  type_name _doc
  flush_interval 1s
</match>

推荐阅读