将 String 转换为 JSON 以便它可以在 Kibana/Elasticsearch 中搜索

Transform String into JSON so that it's searchable in Kibana/Elasticsearch

我在 Windows 机器上有 ElasticsearchFilebeatKibana 运行。 Filebeat log 有正确的日志文件,正在监听路径。当我查看 Kibana 中的数据时,它看起来不错。

我的问题是 message 字段是一个字符串。

一个日志行的例子:

12:58:09.9608 Trace {"message":"No more Excel rows found","level":"Trace","logType":"User","timeStamp":"2020-08-14T12:58:09.9608349+02:00","fingerprint":"226fdd2-e56a-4af4-a7ff-724a1a0fea24","windowsIdentity":"mine","machineName":"NAME-PC","processName":"name","processVersion":"1.0.0.1","jobId":"957ef018-0a14-49d2-8c95-2754479bb8dd","robotName":"NAME-PC","machineId":6,"organizationUnitId":1,"fileName":"GetTransactionData"}

所以我现在想要的是将 String 转换为 JSON 以便可以在 Kibana 中搜索 level 字段。

我已经看过 Filebeat。在那里我试图启用 LogStash 。但随后数据不再进入 Elasticsearch。而且日志文件也不会生成到 LogStash 文件夹中。

然后我通过 install guide 下载了 LogStash,但不幸的是我收到了这条消息:

C:\Users\name\Desktop\logstash-7.8.1\bin>logstash.bat 
Sending
Logstash logs to C:/Users/mine/Desktop/logstash-7.8.1/logs which
is now configured via log4j2.properties ERROR: Pipelines YAML file is
empty. Location:
C:/Users/mine/Desktop/logstash-7.8.1/config/pipelines.yml usage:  
bin/logstash -f CONFIG_PATH [-t] [-r] [] [-w COUNT] [-l LOG]  
bin/logstash --modules MODULE_NAME [-M
"MODULE_NAME.var.PLUGIN_TYPE.PLUGIN_NAME.VARIABLE_NAME=VALUE"] [-t]
[-w COUNT] [-l LOG]   bin/logstash -e CONFIG_STR [-t] [--log.level
fatal|error|warn|info|debug|trace] [-w COUNT] [-l LOG]   bin/logstash
-i SHELL [--log.level fatal|error|warn|info|debug|trace]   bin/logstash -V [--log.level fatal|error|warn|info|debug|trace]  
bin/logstash --help
[2020-08-14T15:07:51,696][ERROR][org.logstash.Logstash    ]
java.lang.IllegalStateException: Logstash stopped processing because
of an error: (SystemExit) exit

编辑:

我尝试只使用 Filebeat。我这里设置:

processors:
  - add_host_metadata: ~
  - add_cloud_metadata: ~
  - add_docker_metadata: ~
  - add_kubernetes_metadata: ~
  - dissect: 
      tokenizer: '"%{event_time} %{loglevel} %{json_message}"' 
      field: "message" 
      target_prefix: "dissect"
  - decode_json_fields: 
      fields: ["json_message"]

但这给了我:

dissect_parsing_error

删除 tokenizer 处的“”的提示很有帮助。然后我得到:

我只是刷新了索引,消息就不见了。不错

但现在的问题是,如何在新字段中筛选某些内容?

消息说,您的管道配置为空。您似乎还没有配置任何管道。 Logstash 可以解决问题 (JSON filter plugin),但 Filebeat 在这里就足够了。如果你不想引入另一个服务,这是更好的选择。

它具有 decode_json_fields 选项,可将事件中包含 JSON 的特定字段转换为 .这里是 documentation.

对于未来的情况,如果您的整个事件是 JSON,则可以在 filebeat 中配置 json.message_key 和相关的 json.* 选项进行解析。

编辑——将 filebeat 片段作为 processors example of dissecting the log line into three fields (event_time, loglevel, json_message). Afterwards the recently extracted field json_message, whose value is a JSON object encoded as a string, will be decoded 添加到 JSON 结构中:

 ... 

filebeat.inputs: 
  - type: log 
    paths: 
      - path to your logfile
  
processors: 
  - dissect: 
      tokenizer: '%{event_time} %{loglevel} %{json_message}' 
      field: "message" 
      target_prefix: "dissect"

  - decode_json_fields: 
      fields: ["dissect.json_message"]
      target: ""

  - drop_fields:
      fields: ["dissect.json_message"]


 ... 

如果你想练习 filebeat 处理器,请尝试设置正确的事件时间戳,从编码的 json 中获取并使用 timestamp processor.

写入@timestamp