Module: Fluent::Mixin::PlainTextFormatter

Defined in:
lib/fluent/mixin/plaintextformatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#add_newlineObject

Returns the value of attribute add_newline.



8
9
10
# File 'lib/fluent/mixin/plaintextformatter.rb', line 8

def add_newline
  @add_newline
end

#default_tagObject

Returns the value of attribute default_tag.



9
10
11
# File 'lib/fluent/mixin/plaintextformatter.rb', line 9

def default_tag
  @default_tag
end

#f_separatorObject

Returns the value of attribute f_separator.



11
12
13
# File 'lib/fluent/mixin/plaintextformatter.rb', line 11

def f_separator
  @f_separator
end

#field_separatorObject

Returns the value of attribute field_separator.



8
9
10
# File 'lib/fluent/mixin/plaintextformatter.rb', line 8

def field_separator
  @field_separator
end

#output_data_typeObject

Returns the value of attribute output_data_type.



7
8
9
# File 'lib/fluent/mixin/plaintextformatter.rb', line 7

def output_data_type
  @output_data_type
end

#output_include_tagObject

Returns the value of attribute output_include_tag.



7
8
9
# File 'lib/fluent/mixin/plaintextformatter.rb', line 7

def output_include_tag
  @output_include_tag
end

#output_include_timeObject

Returns the value of attribute output_include_time.



7
8
9
# File 'lib/fluent/mixin/plaintextformatter.rb', line 7

def output_include_time
  @output_include_time
end

#remove_prefixObject

Returns the value of attribute remove_prefix.



9
10
11
# File 'lib/fluent/mixin/plaintextformatter.rb', line 9

def remove_prefix
  @remove_prefix
end

Instance Method Details

#configure(conf) ⇒ Object



17
18
19
20
21
22
23
24
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
60
61
62
63
64
65
66
67
68
# File 'lib/fluent/mixin/plaintextformatter.rb', line 17

def configure(conf)
  super

  @output_include_time = first_value( Fluent::Config.bool_value(conf['output_include_time']), @output_include_time, true )
  @output_include_tag = first_value( Fluent::Config.bool_value(conf['output_include_tag']), @output_include_tag, true )

  @output_data_type = first_value( conf['output_data_type'], @output_data_type, 'json' )

  @field_separator = first_value( conf['field_separator'], @field_separator, 'TAB' )
  @f_separator = case @field_separator
                 when /SPACE/i then ' '
                 when /COMMA/i then ','
                 when /SOH/i then "\x01"
                 else "\t"
                 end

  @add_newline = first_value( Fluent::Config.bool_value(conf['add_newline']), @add_newline, true )

  @remove_prefix = conf['remove_prefix']
  if @remove_prefix
    @removed_prefix_string = @remove_prefix + '.'
    @removed_length = @removed_prefix_string.length
  end
  if @output_include_tag and @remove_prefix and @remove_prefix.length > 0
    @default_tag = first_value( conf['default_tag'], @default_tag, nil )
    if @default_tag.nil? or @default_tag.length < 1
      raise Fluent::ConfigError, "Missing 'default_tag' with output_include_tag and remove_prefix."
    end
  end

  # default timezone: utc
  if not conf.has_key?('localtime') and not conf.has_key?('utc')
    @localtime = false
  elsif conf.has_key?('localtime')
    @localtime = true
  elsif conf.has_key?('utc')
    @localtime = false
  end
  # mix-in default time formatter (or you can overwrite @timef on your own configure)
  @time_format = first_value( conf['time_format'], @time_format, nil )
  @timef = @output_include_time ? Fluent::TimeFormatter.new(@time_format, @localtime) : nil

  @custom_attributes = if @output_data_type == 'json'
                         nil
                       elsif @output_data_type == 'ltsv'
                         nil
                       elsif @output_data_type =~ /^attr:(.+)$/
                         $1.split(',').map(&:strip).reject(&:empty?)
                       else
                         raise Fluent::ConfigError, "invalid output_data_type:'#{@output_data_type}'"
                       end
end

#first_value(*args) ⇒ Object



13
14
15
# File 'lib/fluent/mixin/plaintextformatter.rb', line 13

def first_value(*args)
  args.reduce{|a,b| (not a.nil?) ? a : b}
end

#format(tag, time, record) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fluent/mixin/plaintextformatter.rb', line 85

def format(tag, time, record)
  if @remove_prefix
    if tag.start_with?(@removed_prefix_string) and tag.length > @removed_length
      tag = tag[@removed_length..-1]
    elsif tag == @remove_prefix
      tag = @default_tag
    end
  end
  time_str = if @output_include_time
               @timef.format(time) + @f_separator
             else
               ''
             end
  tag_str = if @output_include_tag
              tag + @f_separator
            else
              ''
            end
  time_str + tag_str + stringify_record(record) + (@add_newline ? "\n" : '')
end

#stringify_record(record) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fluent/mixin/plaintextformatter.rb', line 70

def stringify_record(record)
  if @custom_attributes.nil?
    case @output_data_type
    when 'json'
      record.to_json
    when 'ltsv'
      LTSV.dump(record)
    end
  else
    @custom_attributes.map{|attr|
      (record[attr] || 'NULL').to_s
    }.join(@f_separator)
  end
end