Class: LogStash::Codecs::JSON

Inherits:
Base show all
Defined in:
lib/logstash/codecs/json.rb

Overview

The codec should be used to decode full json messages. If you are streaming JSON messages delimited by ā€˜nā€™ then see the json_lines codec. Encoding will result in a single json string.

Constant Summary

Constants included from LogStash::Config::Mixin

LogStash::Config::Mixin::CONFIGSORT

Instance Attribute Summary

Attributes included from LogStash::Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Instance Method Summary collapse

Methods inherited from Base

#clone, #flush, #initialize, #on_event, #teardown

Methods included from LogStash::Config::Mixin

#config_init, included

Methods inherited from Plugin

#eql?, #finished, #finished?, #hash, #initialize, #inspect, lookup, #reload, #running?, #shutdown, #teardown, #terminating?, #to_s

Constructor Details

This class inherits a constructor from LogStash::Codecs::Base

Instance Method Details

#decode(data) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/logstash/codecs/json.rb', line 27

def decode(data)
  begin
    yield LogStash::Event.new(JSON.parse(data))
  rescue JSON::ParserError => e
    @logger.info("JSON parse failure. Falling back to plain-text", :error => e, :data => data)
    yield LogStash::Event.new("message" => data)
  end
end

#encode(data) ⇒ Object



37
38
39
# File 'lib/logstash/codecs/json.rb', line 37

def encode(data)
  @on_event.call(data.to_json)
end