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



131
132
133
134
# File 'lib/fluent/plugin_helper/formatter.rb', line 131

def after_shutdown
  formatter_operate(:after_shutdown)
  super
end

#before_shutdownObject



121
122
123
124
# File 'lib/fluent/plugin_helper/formatter.rb', line 121

def before_shutdown
  formatter_operate(:before_shutdown)
  super
end

#closeObject



136
137
138
139
# File 'lib/fluent/plugin_helper/formatter.rb', line 136

def close
  formatter_operate(:close)
  super
end

#configure(conf) ⇒ Object



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

def configure(conf)
  super

  if @formatter_configs
    @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
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 = 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



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

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



126
127
128
129
# File 'lib/fluent/plugin_helper/formatter.rb', line 126

def shutdown
  formatter_operate(:shutdown)
  super
end

#startObject



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

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

#stopObject



116
117
118
119
# File 'lib/fluent/plugin_helper/formatter.rb', line 116

def stop
  super
  formatter_operate(:stop)
end

#terminateObject



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

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