Module: Fluent::PluginHelper::Formatter

Defined in:
lib/fluent/plugin_helper/formatter.rb

Defined Under Namespace

Modules: FormatterParams

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_formattersObject (readonly)

for tests



74
75
76
# File 'lib/fluent/plugin_helper/formatter.rb', line 74

def _formatters
  @_formatters
end

Class Method Details

.included(mod) ⇒ Object



70
71
72
# File 'lib/fluent/plugin_helper/formatter.rb', line 70

def self.included(mod)
  mod.include FormatterParams
end

Instance Method Details

#after_shutdownObject



129
130
131
132
# File 'lib/fluent/plugin_helper/formatter.rb', line 129

def after_shutdown
  formatter_operate(:after_shutdown)
  super
end

#before_shutdownObject



119
120
121
122
# File 'lib/fluent/plugin_helper/formatter.rb', line 119

def before_shutdown
  formatter_operate(:before_shutdown)
  super
end

#closeObject



134
135
136
137
# File 'lib/fluent/plugin_helper/formatter.rb', line 134

def close
  formatter_operate(:close)
  super
end

#configure(conf) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fluent/plugin_helper/formatter.rb', line 82

def configure(conf)
  super

  @formatter_configs.each do |section|
    if @_formatters[section.usage]
      raise Fluent::ConfigError, "duplicated formatter configured: #{section.usage}"
    end
    formatter = Plugin.new_formatter(section[:@type], parent: self)
    formatter.configure(section.corresponding_config_element)
    @_formatters[section.usage] = formatter
  end
end

#formatter_create(usage: '', type: nil, conf: nil, default_type: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fluent/plugin_helper/formatter.rb', line 25

def formatter_create(usage: '', type: nil, conf: nil, default_type: nil)
  formatter = @_formatters[usage]
  return formatter if formatter && !type && !conf

  type = if type
           type
         elsif conf && conf.respond_to?(:[])
           raise Fluent::ConfigError, "@type is required in <format>" unless conf['@type']
           conf['@type']
         elsif default_type
           default_type
         else
           raise ArgumentError, "BUG: both type and conf are not specified"
         end
  formatter = Fluent::Plugin.new_formatter(type, parent: self)
  config = case conf
           when Fluent::Config::Element
             conf
           when Hash
             # in code, programmer may use symbols as keys, but Element needs strings
             conf = Hash[conf.map{|k,v| [k.to_s, v]}]
             Fluent::Config::Element.new('format', usage, conf, [])
           when nil
             Fluent::Config::Element.new('format', usage, {}, [])
           else
             raise ArgumentError, "BUG: conf must be a Element, Hash (or unspecified), but '#{conf.class}'"
           end
  formatter.configure(config)
  if @_formatters_started
    formatter.start
  end

  @_formatters[usage] = formatter
  formatter
end

#formatter_operate(method_name, &block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/fluent/plugin_helper/formatter.rb', line 103

def formatter_operate(method_name, &block)
  @_formatters.each_pair do |usage, formatter|
    begin
      formatter.__send__(method_name)
      block.call(formatter) if block_given?
    rescue => e
      log.error "unexpected error while #{method_name}", usage: usage, formatter: formatter, error: e
    end
  end
end

#initializeObject



76
77
78
79
80
# File 'lib/fluent/plugin_helper/formatter.rb', line 76

def initialize
  super
  @_formatters_started = false
  @_formatters = {} # usage => formatter
end

#shutdownObject



124
125
126
127
# File 'lib/fluent/plugin_helper/formatter.rb', line 124

def shutdown
  formatter_operate(:shutdown)
  super
end

#startObject



95
96
97
98
99
100
101
# File 'lib/fluent/plugin_helper/formatter.rb', line 95

def start
  super
  @_formatters_started = true
  @_formatters.each_pair do |usage, formatter|
    formatter.start
  end
end

#stopObject



114
115
116
117
# File 'lib/fluent/plugin_helper/formatter.rb', line 114

def stop
  super
  formatter_operate(:stop)
end

#terminateObject



139
140
141
142
143
144
# File 'lib/fluent/plugin_helper/formatter.rb', line 139

def terminate
  formatter_operate(:terminate)
  @_formatters_started = false
  @_formatters = {}
  super
end