Module: FluentExt::PlainTextFormatterMixin

Included in:
Fluent::HoopOutput
Defined in:
lib/fluent/plugin/out_hoop.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#add_newlineObject

Returns the value of attribute add_newline.



6
7
8
# File 'lib/fluent/plugin/out_hoop.rb', line 6

def add_newline
  @add_newline
end

#default_tagObject

Returns the value of attribute default_tag.



7
8
9
# File 'lib/fluent/plugin/out_hoop.rb', line 7

def default_tag
  @default_tag
end

#field_separatorObject

Returns the value of attribute field_separator.



6
7
8
# File 'lib/fluent/plugin/out_hoop.rb', line 6

def field_separator
  @field_separator
end

#output_data_typeObject

config_param :output_data_type, :string, :default => ‘json’ # or ‘attr:field’ or ‘attr:field1,field2,field3(…)’



5
6
7
# File 'lib/fluent/plugin/out_hoop.rb', line 5

def output_data_type
  @output_data_type
end

#output_include_tagObject

config_param :output_data_type, :string, :default => ‘json’ # or ‘attr:field’ or ‘attr:field1,field2,field3(…)’



5
6
7
# File 'lib/fluent/plugin/out_hoop.rb', line 5

def output_include_tag
  @output_include_tag
end

#output_include_timeObject

config_param :output_data_type, :string, :default => ‘json’ # or ‘attr:field’ or ‘attr:field1,field2,field3(…)’



5
6
7
# File 'lib/fluent/plugin/out_hoop.rb', line 5

def output_include_time
  @output_include_time
end

#remove_prefixObject

Returns the value of attribute remove_prefix.



7
8
9
# File 'lib/fluent/plugin/out_hoop.rb', line 7

def remove_prefix
  @remove_prefix
end

Instance Method Details

#configure(conf) ⇒ Object



9
10
11
12
13
14
15
16
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/fluent/plugin/out_hoop.rb', line 9

def configure(conf)
  super

  @output_include_time = Fluent::Config.bool_value(conf['output_include_time'])
  @output_include_time = true if @output_include_time.nil?

  @output_include_tag = Fluent::Config.bool_value(conf['output_include_tag'])
  @output_include_tag = true if @output_include_tag.nil?

  @output_data_type = conf['output_data_type']
  @output_data_type = 'json' if @output_data_type.nil?

  @field_separator = case conf['field_separator']
                     when 'SPACE' then ' '
                     when 'COMMA' then ','
                     else "\t"
                     end
  @add_newline = Fluent::Config.bool_value(conf['add_newline'])
  if @add_newline.nil?
    @add_newline = true
  end

  @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 = conf['default_tag']
    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 conf['localtime'].nil? and conf['utc'].nil?
    @utc = true
    @localtime = false
  elsif not @localtime and not @utc
    @utc = true
    @localtime = false
  end
  # mix-in default time formatter (or you can overwrite @timef on your own configure)
  @timef = @output_include_time ? Fluent::TimeFormatter.new(@time_format, @localtime) : nil

  @custom_attributes = []
  if @output_data_type == 'json'
    self.instance_eval {
      def stringify_record(record)
        record.to_json
      end
    }
  elsif @output_data_type =~ /^attr:(.*)$/
    @custom_attributes = $1.split(',')
    if @custom_attributes.size > 1
      self.instance_eval {
        def stringify_record(record)
          @custom_attributes.map{|attr| (record[attr] || 'NULL').to_s}.join(@field_separator)
        end
      }
    elsif @custom_attributes.size == 1
      self.instance_eval {
        def stringify_record(record)
          (record[@custom_attributes[0]] || 'NULL').to_s
        end
      }
    else
      raise Fluent::ConfigError, "Invalid attributes specification: '#{@output_data_type}', needs one or more attributes."
    end
  else
    raise Fluent::ConfigError, "Invalid output_data_type: '#{@output_data_type}'. specify 'json' or 'attr:ATTRIBUTE_NAME' or 'attr:ATTR1,ATTR2,...'"
  end

  if @output_include_time and @output_include_tag
    if @add_newline and @remove_prefix
      self.instance_eval {
        def format(tag,time,record)
          if (tag[0, @removed_length] == @removed_prefix_string and tag.length > @removed_length) or
              tag == @remove_prefix
            tag = tag[@removed_length..-1] || @default_tag
          end
          @timef.format(time) + @field_separator + tag + @field_separator + stringify_record(record) + "\n"
        end
      }
    elsif @add_newline
      self.instance_eval {
        def format(tag,time,record)
          @timef.format(time) + @field_separator + tag + @field_separator + stringify_record(record) + "\n"
        end
      }
    elsif @remove_prefix
      self.instance_eval {
        def format(tag,time,record)
          if (tag[0, @removed_length] == @removed_prefix_string and tag.length > @removed_length) or
              tag == @remove_prefix
            tag = tag[@removed_length..-1] || @default_tag
          end
          @timef.format(time) + @field_separator + tag + @field_separator + stringify_record(record)
        end
      }
    else
      self.instance_eval {
        def format(tag,time,record)
          @timef.format(time) + @field_separator + tag + @field_separator + stringify_record(record)
        end
      }
    end
  elsif @output_include_time
    if @add_newline
      self.instance_eval {
        def format(tag,time,record);
          @timef.format(time) + @field_separator + stringify_record(record) + "\n"
        end
      }
    else
      self.instance_eval {
        def format(tag,time,record);
          @timef.format(time) + @field_separator + stringify_record(record)
        end
      }
    end
  elsif @output_include_tag
    if @add_newline and @remove_prefix
      self.instance_eval {
        def format(tag,time,record)
          if (tag[0, @removed_length] == @removed_prefix_string and tag.length > @removed_length) or
              tag == @remove_prefix
            tag = tag[@removed_length..-1] || @default_tag
          end
          tag + @field_separator + stringify_record(record) + "\n"
        end
      }
    elsif @add_newline
      self.instance_eval {
        def format(tag,time,record)
          tag + @field_separator + stringify_record(record) + "\n"
        end
      }
    elsif @remove_prefix
      self.instance_eval {
        def format(tag,time,record)
          if (tag[0, @removed_length] == @removed_prefix_string and tag.length > @removed_length) or
              tag == @remove_prefix
            tag = tag[@removed_length..-1] || @default_tag
          end
          tag + @field_separator + stringify_record(record)
        end
      }
    else
      self.instance_eval {
        def format(tag,time,record)
          tag + @field_separator + stringify_record(record)
        end
      }
    end
  else # without time, tag
    if @add_newline
      self.instance_eval {
        def format(tag,time,record);
          stringify_record(record) + "\n"
        end
      }
    else
      self.instance_eval {
        def format(tag,time,record);
          stringify_record(record)
        end
      }
    end
  end
end

#format(tag, time, record) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/fluent/plugin/out_hoop.rb', line 185

def format(tag, time, record)
  if tag == @remove_prefix or (tag[0, @removed_length] == @removed_prefix_string and tag.length > @removed_length)
    tag = tag[@removed_length..-1] || @default_tag
  end
  time_str = if @output_include_time
               @timef.format(time) + @field_separator
             else
               ''
             end
  tag_str = if @output_include_tag
              tag + @field_separator
            else
              ''
            end
  time_str + tag_str + stringify_record(record) + "\n"
end

#stringify_record(record) ⇒ Object



181
182
183
# File 'lib/fluent/plugin/out_hoop.rb', line 181

def stringify_record(record)
  record.to_json
end