Class: LogStash::Codecs::JSONLines

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

Overview

This codec will decode streamed JSON that is newline delimited. For decoding line-oriented JSON payload in the redis or file inputs, for example, use the json codec instead. Encoding will emit a single JSON string ending in a ‘n`

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ 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(LogStash::Json.load(event["message"]))
    rescue LogStash::Json::ParserError => e
      @logger.info("JSON parse failure. Falling back to plain-text", :error => e, :data => data)
      yield LogStash::Event.new("message" => event["message"], "tags" => "_jsonparsefailure")
    end
  end
end

#encode(event) ⇒ Object



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

def encode(event)
  # 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(event, event.to_json + NL)
end