7

我有这样的日志:

{"logId":"57aaf6c8d32fb","clientIp":"127.0.0.1","time":"03:11:29 pm","uniqueSubId":"57aaf6c98963b","channelName":"JSPC","apiVersion":"v1","modulName":null,"actionName":"apiRequest","typeOfError":"","statusCode":"","message":"In Auth","exception":"In Auth","logType":"Info"}

{"logId":"57aaf6c8d32fb","clientIp":"127.0.0.1","time":"03:11:29 pm","uniqueSubId":"57aaf6c987206","channelName":"JSPC","apiVersion":"v2","modulName":null,"actionName":"performV2","typeOfError":"","statusCode":"","message":"in inbox api v2 5","exception":"in inbox api v2 5","logType":"Info"}

我想把他们推到kibana. 我正在使用 filebeat 将数据发送到 logstash,使用以下配置:

filebeat.yml

 ### Logstash as output
logstash:
# The Logstash hosts
hosts: ["localhost:5044"]

# Number of workers per Logstash host.
#worker: 1

现在使用以下配置,我想更改编解码器类型:

input {

     beats {
     port => 5000
     tags => "beats"
     codec => "json_lines"
     #ssl  => true
     #ssl_certificate => "/opt/filebeats/logs.example.com.crt"
     #ssl_key => "/opt/filebeats/logs.example.com.key"
     }


     syslog {
        type => "syslog"
        port => "5514"

    }

}

但是,我仍然得到字符串格式的日志:

"消息": "{\"logId\":\"57aaf6c96224b\",\"clientIp\":\"127.0.0.1\",\"time\":\"03:11:29 pm\",\ "channelName\":\"JSPC\",\"apiVersion\":null,\"modulName\":null,\"actionName\":\"404\",\"typeOfError\":\"EXCEPTION\" ,\"statusCode\":0,\"message\":\"404 页面遇到 http:\/\/localjs.com\/uploads\/NonScreenedImages\/profilePic120\/16\/29\/15997002iicee52ad041fed55e952d4e4e163d5972ii4c41f881\510542330.jcc1 ",\"logType\":\"错误\"}",

请帮我解决这个问题。

4

3 回答 3

8

要解析从 Filebeat 发送的 Logstash 中的 JSON 日志行,您需要使用json 过滤器而不是编解码器。这是因为 Filebeat 将其数据作为 JSON 发送,并且日志行的内容包含在该message字段中。

Logstash 配置:

input {
  beats {
    port => 5044
  }   
}   

filter {
  if [tags][json] {
    json {
      source => "message"
    }   
  }   
}   

output {
  stdout { codec => rubydebug { metadata => true } } 
}

文件节拍配置:

filebeat:
  prospectors:
    - paths:
        - my_json.log
      fields_under_root: true
      fields:
        tags: ['json']
output:
  logstash:
    hosts: ['localhost:5044']

在 Filebeat 配置中,我为事件添加了一个“json”标签,以便可以有条件地将 json 过滤器应用于数据。

Filebeat 5.0 能够在不使用 Logstash 的情况下解析 JSON,但目前仍是 alpha 版本。这篇题为“使用 Filebeat 进行结构化日志记录”的博文演示了如何使用 Filebeat 5.0 解析 JSON。

于 2016-08-10T13:45:01.077 回答
4

从 FileBeat 5.x 你可以在不使用 Logstash 的情况下做到这一点。

文件节拍配置:

filebeat.prospectors:
- input_type: log
  paths: ["YOUR_LOG_FILE_DIR/*"]
  json.message_key: logId
  json.keys_under_root: true

output.elasticsearch:
  hosts: ["<HOSTNAME:PORT>"]
  template.name: filebeat
  template.path: filebeat.template.json

Filebeat 比 Logstash 更轻量级。此外,即使您需要插入到 elasticsearch 版本 2.x,您也可以使用 FileBeat 5.x 的此功能 可以在此处找到真实示例

于 2016-12-27T13:10:08.860 回答
2

我已经在互联网上搜索了与您遇到的完全相同的问题,并尝试了各种建议,包括上述建议。但是,没有任何帮助,所以我以老式的方式进行了操作。我继续阅读有关 filebeat 配置的 elasticsearch文档

以及所需的一切(无需在 logstash 中配置过滤器)

文件节拍配置:

filebeat.prospectors:
- input_type: log
  document_type: #whatever your type is, this is optional
  json.keys_under_root: true
  paths:
    - #your path goes here

keys_under_root

将嵌套的 json 键复制到输出文档的顶层。

我的 filebeat 版本是 5.2.2。

于 2017-03-08T14:25:40.870 回答