class LogStash::Codecs::JSONLines

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'

Public Class Methods

new(params={}) click to toggle source
Calls superclass method LogStash::Codecs::Base::new
# File lib/logstash/codecs/json_lines.rb, line 26
def initialize(params={})
  super(params)
  @lines = LogStash::Codecs::Line.new
  @lines.charset = @charset
end

Public Instance Methods

decode(data) { |event(parse)| ... } click to toggle source
# 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) click to toggle source
# 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