【问题标题】:Using JSON with LogStash将 JSON 与 LogStash 一起使用
【发布时间】:2014-04-08 15:39:34
【问题描述】:

我在这里疯了。我有一个将日志写入文件的应用程序。每个日志条目都是一个 JSON 对象。我的 .json 文件示例如下所示:

{"Property 1":"value A","Property 2":"value B"}
{"Property 1":"value x","Property 2":"value y"}

我正在拼命地将日志条目放入 LogStash。为此,我创建了以下 LogStash 配置文件:

input {
  file {
    type => "json"
    path => "/logs/mylogs.log"
    codec => "json"
  }
}
output {
  file {
    path => "/logs/out.log"
  }
}

现在,我正在手动将记录添加到 mylogs.log 以尝试使其正常工作。但是,它们在标准输出中显得很奇怪。当我查看 open out.log 时,我看到如下内容:

{"message":"\"Property 1\":\"value A\", \"Property 2\":\"value B\"}","@version":"1","@timestamp":"2014-04-08T15:33:07.519Z","type":"json","host":"ip-[myAddress]","path":"/logs/mylogs.log"}

因此,如果我将消息发送到 ElasticSearch,我不会收到这些字段。相反,我得到了一个混乱的混乱。我需要我的财产仍然是财产。我不希望它们塞进消息部分或输出中。我有一种预感,这与编解码器有关。然而,我不确定。我不确定是否应该更改 logstash 输入配置上的编解码器。或者,如果我应该更改输出配置的输入。

【问题讨论】:

    标签: json logstash


    【解决方案1】:

    尝试删除json codec 并添加json filter

    input {
      file {
        type => "json"
        path => "/logs/mylogs.log"
      }
    }
    filter{
        json{
            source => "message"
        }
    }
    output {
      file {
        path => "/logs/out.log"
      }
    }
    

    您不需要 json 编解码器,因为您不想解码源 JSON,但您想过滤输入以仅在 @message 字段中获取 JSON 数据。

    【讨论】:

    • 甜蜜!那行得通。除了,它并没有帮助我超越我真正的 JSON :(。我会给你答案的。我在 stackoverflow.com/questions/22944168/… 打开了第二个问题
    • 嗨!我使用了这个 logstash 配置文件 input { tcp { port => '9563' } } filter{ json{ source => "message" } } output { elasticsearch { hosts => [ "localhost:9200" ] } },但仍在弹性搜索中,我的 json 作为字符串作为 message:"{ \"container\": \"DOCKER\", \"msg\" : \"From python 26 oct\" }" 放在消息字段中。我无法将 containermsg 作为 kibana 中的不同字段。我在 kibana 中有这个额外的字段说 tags:_jsonparsefailure 。我的 json 是 { "container": "DOCKER", "msg" : "From python 26 oct" }
    • logstash 日志{:timestamp=>"2016-10-26T03:12:51.814000-0400", :message=>"Parsed JSON object/hash requires a target configuration option", :source=>"message", :raw=>"\"{ \\\"container\\\": \\\"DOCKER\\\", \\\"msg\\\" : \\\"From python 26 oct\\\" }\"", :level=>:warn}
    • @vzamanillo json 过滤器仍然会对 JSON 进行不需要/不必要的解析,不是吗?我也无法让默认目标执行它应该做的事情,即替换事件根(v 5.6.1),但这可能是一个单独的问题。
    【解决方案2】:

    如果未指定 json 编解码器,默认情况下 tcp 会将所有内容放入消息字段。

    我们指定json编解码器后消息字段的_jsonparsefailure的解决方法也可以通过执行以下操作来纠正:

    input {
      tcp {
        port => '9563'
      }
    }
    filter{
      json{
        source => "message"
        target => "myroot"
      }
      json{
        source => "myroot"
      }
    
    }
    output {
        elasticsearch {
          hosts => [ "localhost:9200" ]
        }
    }
    

    它将消息字段解析为正确的 json 字符串到字段 myroot 然后解析 myroot 以生成 json。

    我们可以去掉像message这样的冗余字段

    filter {
      json {
        source => "message"
        remove_field => ["message"]
      }
    }
    

    【讨论】:

      【解决方案3】:

      试试这个:

      filter {
        json {
              source => "message"
              target => "jsoncontent" # with multiple layers structure
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多