Class: Fluent::TextFormatter::SprintfFormatter

Inherits:
Formatter
  • Object
show all
Includes:
Configurable, HandleTagAndTimeMixin
Defined in:
lib/fluent/plugin/formatter_sprintf.rb

Instance Method Summary collapse

Constructor Details

#initializeSprintfFormatter

Returns a new instance of SprintfFormatter.



11
12
13
# File 'lib/fluent/plugin/formatter_sprintf.rb', line 11

def initialize
  super
end

Instance Method Details

#configure(conf) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fluent/plugin/formatter_sprintf.rb', line 15

def configure(conf)
  super
  @time_format = @time_format || '%Y-%m-%d %H:%M:%S'
  r = /\$\{([^}]+)\}/
  @keys = @sprintf_format.scan(r).map{ |key| key.first }
  @sprintf_format = @sprintf_format.gsub(/\$\{([^}]+)\}/, '%s')
  begin
    @sprintf_format % @keys
  rescue ArgumentError => e
    raise Fluent::ConfigError, "formatter_sprintf: @sprintf_format is can't include '%'"
  end
end

#format(tag, time, record) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fluent/plugin/formatter_sprintf.rb', line 28

def format(tag, time, record)
  values = @keys.map{ |key| 
    if key == 'tag'
      tag
    elsif key == 'time'
      Time.at(time).strftime(@time_format)
    else
      record[key] 
    end
  }
  @sprintf_format % values
end