Module: Fluent::Compat::FormatterUtils

Defined in:
lib/fluent/compat/formatter_utils.rb

Defined Under Namespace

Modules: InjectMixin

Constant Summary collapse

INJECT_PARAMS =
Fluent::PluginHelper::CompatParameters::INJECT_PARAMS
FORMATTER_PARAMS =
Fluent::PluginHelper::CompatParameters::FORMATTER_PARAMS

Class Method Summary collapse

Class Method Details

.convert_formatter_conf(conf) ⇒ Object



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fluent/compat/formatter_utils.rb', line 32

def self.convert_formatter_conf(conf)
  return if conf.elements(name: 'inject').first || conf.elements(name: 'format').first

  inject_params = {}
  INJECT_PARAMS.each do |older, newer|
    next unless newer
    if conf.has_key?(older)
      inject_params[newer] = conf[older]
    end
  end

  if conf.has_key?('include_time_key') && Fluent::Config.bool_value(conf['include_time_key'])
    inject_params['time_key'] ||= 'time'
    inject_params['time_type'] ||= 'string'
  end
  if conf.has_key?('time_as_epoch') && Fluent::Config.bool_value(conf['time_as_epoch'])
    inject_params['time_type'] = 'unixtime'
  end
  if conf.has_key?('localtime') || conf.has_key?('utc')
    if conf.has_key?('localtime') && conf.has_key?('utc')
      raise Fluent::ConfigError, "both of utc and localtime are specified, use only one of them"
    elsif conf.has_key?('localtime')
      inject_params['localtime'] = Fluent::Config.bool_value(conf['localtime'])
    elsif conf.has_key?('utc')
      inject_params['localtime'] = !(Fluent::Config.bool_value(conf['utc']))
      # Specifying "localtime false" means using UTC in TimeFormatter
      # And specifying "utc" is different from specifying "timezone +0000"(it's not always UTC).
      # There are difference between "Z" and "+0000" in timezone formatting.
      # TODO: add kwargs to TimeFormatter to specify "using localtime", "using UTC" or "using specified timezone" in more explicit way
    end
  end

  if conf.has_key?('include_tag_key') && Fluent::Config.bool_value(conf['include_tag_key'])
    inject_params['tag_key'] ||= 'tag'
  end

  unless inject_params.empty?
    conf.elements << Fluent::Config::Element.new('inject', '', inject_params, [])
  end

  formatter_params = {}
  FORMATTER_PARAMS.each do |older, newer|
    next unless newer
    if conf.has_key?(older)
      formatter_params[newer] = conf[older]
    end
  end
  unless formatter_params.empty?
    conf.elements << Fluent::Config::Element.new('format', '', formatter_params, [])
  end
end