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:

source,ruby

filter {

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

}

Instance Method Summary collapse

Instance Method Details

#filter(event) ⇒ Object



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

def filter(event)
  

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

  begin
    event[@target] = LogStash::Json.dump(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



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

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