Class: LogStash::Codecs::Base

Inherits:
Plugin
  • Object
show all
Includes:
LogStash::Config::Mixin
Defined in:
lib/logstash/codecs/base.rb

Constant Summary

Constants included from LogStash::Config::Mixin

LogStash::Config::Mixin::PLUGIN_VERSION_0_9_0, LogStash::Config::Mixin::PLUGIN_VERSION_1_0_0

Constants included from Util::SubstitutionVariables

Util::SubstitutionVariables::SUBSTITUTION_PLACEHOLDER_REGEX

Constants inherited from Plugin

Plugin::NL

Instance Attribute Summary

Attributes included from LogStash::Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#execution_context, #params

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LogStash::Config::Mixin

#config_init, included

Methods included from Util::SubstitutionVariables

#deep_replace, #replace_placeholders

Methods inherited from Plugin

#config_name, #debug_info, #do_close, #eql?, #hash, #id, #inspect, lookup, #metric, #metric=, #plugin_metadata, #reloadable?, reloadable?, #to_s

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
20
# File 'lib/logstash/codecs/base.rb', line 15

def initialize(params={})
  super
  config_init(@params)
  register if respond_to?(:register)
  setup_multi_encode!
end

Class Method Details

.plugin_typeObject



11
12
13
# File 'lib/logstash/codecs/base.rb', line 11

def self.plugin_type
  "codec"
end

Instance Method Details

#cloneObject



78
79
80
# File 'lib/logstash/codecs/base.rb', line 78

def clone
  return self.class.new(params)
end

#closeObject



62
# File 'lib/logstash/codecs/base.rb', line 62

def close; end

#decode(data) ⇒ Object Also known as: <<



23
24
25
# File 'lib/logstash/codecs/base.rb', line 23

def decode(data)
  raise "#{self.class}#decode must be overidden"
end

#encode(event) ⇒ Object

DEPRECATED: Prefer defining encode_sync or multi_encode



31
32
33
34
# File 'lib/logstash/codecs/base.rb', line 31

def encode(event)
  encoded = multi_encode([event])
  encoded.each {|event,data| @on_event.call(event,data) }
end

#flush(&block) ⇒ Object



71
72
73
74
75
# File 'lib/logstash/codecs/base.rb', line 71

def flush(&block)
  # does nothing by default.
  # if your codec needs a flush method (like you are spooling things)
  # you must implement this.
end

#multi_encode(events) ⇒ Object

Relies on the codec being synchronous (which they all are!) We need a better long term design here, but this is an improvement over the current API for shared plugins It is best if the codec implements this directly



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/logstash/codecs/base.rb', line 41

def multi_encode(events)
  if @has_encode_sync
    events.map {|event| [event, self.encode_sync(event)]}
  else
    batch = Thread.current[:logstash_output_codec_batch] ||= []
    batch.clear

    events.each {|event| self.encode(event) }
    batch
  end
end

#on_event(&block) ⇒ Object



66
67
68
# File 'lib/logstash/codecs/base.rb', line 66

def on_event(&block)
  @on_event = block
end

#setup_multi_encode!Object



53
54
55
56
57
58
59
# File 'lib/logstash/codecs/base.rb', line 53

def setup_multi_encode!
  @has_encode_sync = self.methods.include?(:encode_sync)

  on_event do |event, data|
    Thread.current[:logstash_output_codec_batch] << [event, data]
  end
end