Class: LogStash::Codecs::JSONLines

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

Overview

This codec will decode streamed JSON that is newline delimited. For decoding JSON payload in the redis input for example, use the json codec instead. Encoding will emit a single JSON string ending in a ā€˜nā€™

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, #on_event, #teardown

Methods included from LogStash::Config::Mixin

#config_init, included

Methods inherited from Plugin

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

Constructor Details

#initialize(params = {}) ⇒ JSONLines

Returns a new instance of JSONLines.



26
27
28
29
30
# File 'lib/logstash/codecs/json_lines.rb', line 26

def initialize(params={})
  super(params)
  @lines = LogStash::Codecs::Line.new
  @lines.charset = @charset
end

Instance Method Details

#decode(data) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/logstash/codecs/json_lines.rb', line 33

def decode(data)

  @lines.decode(data) do |event|
    begin
      yield LogStash::Event.new(JSON.parse(event["message"]))
    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
end

#encode(data) ⇒ Object



46
47
48
49
50
# File 'lib/logstash/codecs/json_lines.rb', line 46

def encode(data)
  # Tack on a \n for now because previously most of logstash's JSON
  # outputs emitted one per line, and whitespace is OK in json.
  @on_event.call(data.to_json + "\n")
end