Class: LogStash::Codecs::JSON

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

Overview

This codec may be used to decode (via inputs) and encode (via outputs) full JSON messages. If you are streaming JSON messages delimited by ‘n’ then see the ‘json_lines` codec.

Encoding will result in a compact JSON representation (no line terminators or indentation)

If this codec recieves a payload from an input that is not valid JSON, then it will fall back to plain text and add a tag ‘_jsonparsefailure`. Upon a JSON failure, the payload will be stored in the `message` field.

Instance Method Summary collapse

Instance Method Details

#decode(data) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/logstash/codecs/json.rb', line 37

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

#encode(event) ⇒ Object



48
49
50
# File 'lib/logstash/codecs/json.rb', line 48

def encode(event)
  @on_event.call(event, event.to_json)
end

#registerObject



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

def register
  @converter = LogStash::Util::Charset.new(@charset)
  @converter.logger = @logger
end