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.



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

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

#null_valueObject

Returns the value of attribute null_value.



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

def null_value
  @null_value
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

#suppress_log_broken_stringObject

Returns the value of attribute suppress_log_broken_string.



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

def suppress_log_broken_string
  @suppress_log_broken_string
end

Instance Method Details

#configure(conf) ⇒ Object



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
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fluent/mixin/plaintextformatter.rb', line 20

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

  @null_value = first_value( conf['null_value'], @null_value, 'NULL' )

  # 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

  @suppress_log_broken_string = first_value(
    Fluent::Config.bool_value(conf['suppress_log_broken_string']),
    @suppress_log_broken_string,
    false
  )
end

#first_value(*args) ⇒ Object



16
17
18
# File 'lib/fluent/mixin/plaintextformatter.rb', line 16

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

#format(tag, time, record) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/fluent/mixin/plaintextformatter.rb', line 96

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
  begin
    time_str + tag_str + stringify_record(record) + (@add_newline ? "\n" : '')
  rescue JSON::GeneratorError => e
    # partial character in source, but hit end
    # source sequence is illegal/malformed utf-8
    unless @suppress_log_broken_string
      if self.respond_to?(:log)
        log.warn e.message + ", ignored", :error_class => e.class, :tag => tag, :record => record.inspect # quote explicitly
      else
        $log.warn e.message + ", ignored", :error_class => e.class, :tag => tag, :record => record.inspect # quote explicitly
      end
    end
    ''
  rescue ArgumentError => e
    raise unless e.message == 'invalid byte sequence in UTF-8'
    unless @suppress_log_broken_string
      if self.respond_to?(:log)
        log.warn e.message + ", ignored", :error_class => e.class, :tag => tag, :record => record.inspect # quote explicitly
      else
        $log.warn e.message + ", ignored", :error_class => e.class, :tag => tag, :record => record.inspect # quote explicitly
      end
    end
    ''
  end
end

#stringify_record(record) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fluent/mixin/plaintextformatter.rb', line 81

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].nil? ? @null_value : record[attr].to_s
    }.join(@f_separator)
  end
end