Class: LogStash::Filters::JSONEncode

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/filters/json_encode.rb

Overview

JSON encode filter. Takes a field and serializes it into JSON

If no target is specified, the source field is overwritten with the JSON text.

For example, if you have a field named ‘foo’, and you want to store the JSON encoded string in ‘bar’, do this:

filter {
  json_encode {
    source => "foo"
    target => "bar"
  }
}

Constant Summary

Constants inherited from Base

Base::RESERVED

Constants included from Config::Mixin

Config::Mixin::CONFIGSORT

Instance Attribute Summary

Attributes included from Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Instance Method Summary collapse

Methods inherited from Base

#execute, #initialize, #threadsafe?

Methods included from 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::Filters::Base

Instance Method Details

#filter(event) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/logstash/filters/json_encode.rb', line 37

def filter(event)
  return unless filter?(event)

  @logger.debug("Running JSON encoder", :event => event)

  begin
    event[@target] = JSON.pretty_generate(event[@source])
    filter_matched(event)
  rescue => e
    event.tag "_jsongeneratefailure"
    @logger.warn("Trouble encoding JSON", :source => @source, :raw => event[@source].inspect, :exception => e)
  end

  @logger.debug? && @logger.debug("Event after JSON encoder", :event => event)
end

#registerObject



32
33
34
# File 'lib/logstash/filters/json_encode.rb', line 32

def register
  @target = @source if @target.nil?
end