Sha256: 0f1182947a7236f9ee36adc6ed836ec0359f34fcf29d09367a41e8722f072dbc

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

# encoding: utf-8
require "logstash/filters/base"
require "logstash/namespace"

# 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"
#       }
#     }
class LogStash::Filters::JSONEncode < LogStash::Filters::Base

  config_name "json_encode"
  milestone 2

  # The field to convert to JSON.
  config :source, :validate => :string, :required => true

  # The field to write the JSON into. If not specified, the source
  # field will be overwritten.
  config :target, :validate => :string

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

  public
  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 # def filter
end # class LogStash::Filters::JSONEncode

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logstash-lib-1.3.2 lib/logstash/filters/json_encode.rb