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::ALLOW_ENV_FLAG, LogStash::Config::Mixin::ENV_PLACEHOLDER_REGEX, LogStash::Config::Mixin::PLUGIN_VERSION_0_9_0, LogStash::Config::Mixin::PLUGIN_VERSION_1_0_0

Constants inherited from Plugin

Plugin::NL

Instance Attribute Summary

Attributes included from LogStash::Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Instance Method Summary collapse

Methods included from LogStash::Config::Mixin

#config_init, included, #replace_env_placeholders

Methods inherited from Plugin

#debug_info, #do_close, #eql?, #hash, #inspect, lookup, #metric, #to_s

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



12
13
14
15
16
17
# File 'lib/logstash/codecs/base.rb', line 12

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

Instance Method Details

#cloneObject



75
76
77
# File 'lib/logstash/codecs/base.rb', line 75

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

#closeObject



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

def close; end

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



20
21
22
# File 'lib/logstash/codecs/base.rb', line 20

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

#encode(event) ⇒ Object

DEPRECATED: Prefer defining encode_sync or multi_encode



28
29
30
31
# File 'lib/logstash/codecs/base.rb', line 28

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

#flush(&block) ⇒ Object



68
69
70
71
72
# File 'lib/logstash/codecs/base.rb', line 68

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



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/logstash/codecs/base.rb', line 38

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



63
64
65
# File 'lib/logstash/codecs/base.rb', line 63

def on_event(&block)
  @on_event = block
end

#setup_multi_encode!Object



50
51
52
53
54
55
56
# File 'lib/logstash/codecs/base.rb', line 50

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