Class: FluentExt::TextParser

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/fixed_parser.rb

Defined Under Namespace

Classes: ApacheParser, CSVParser, GenericParser, JSONParser, LabeledTSVParser, RegexpParser, TSVParser, ValuesParser

Constant Summary collapse

TEMPLATE_FACTORIES =
{
  'apache' => Proc.new { RegexpParser.new(/^(?<host>[^ ]*) [^ ]* (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)")?$/, {'time_format'=>"%d/%b/%Y:%H:%M:%S %z"}) },
  'apache2' => Proc.new { ApacheParser.new },
  'nginx' => Proc.new { RegexpParser.new(/^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)")?$/,  {'time_format'=>"%d/%b/%Y:%H:%M:%S %z"}) },
  'syslog' => Proc.new { RegexpParser.new(/^(?<time>[^ ]*\s*[^ ]* [^ ]*) (?<host>[^ ]*) (?<ident>[a-zA-Z0-9_\/\.\-]*)(?:\[(?<pid>[0-9]+)\])?[^\:]*\: *(?<message>.*)$/, {'time_format'=>"%b %d %H:%M:%S"}) },
  'json' => Proc.new { JSONParser.new },
  'csv' => Proc.new { CSVParser.new },
  'tsv' => Proc.new { TSVParser.new },
  'ltsv' => Proc.new { LabeledTSVParser.new },
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTextParser

Returns a new instance of TextParser.



200
201
202
# File 'lib/fluent/plugin/fixed_parser.rb', line 200

def initialize
  @parser = nil
end

Class Method Details

.register_template(name, regexp_or_proc, time_format = nil) ⇒ Object



189
190
191
192
193
194
195
196
197
198
# File 'lib/fluent/plugin/fixed_parser.rb', line 189

def self.register_template(name, regexp_or_proc, time_format=nil)

  factory = if regexp_or_proc.is_a?(Regexp)
              regexp = regexp_or_proc
              Proc.new { RegexpParser.new(regexp, {'time_format'=>time_format}) }
            else
              Proc.new { proc }
            end
  TEMPLATE_FACTORIES[name] = factory
end

Instance Method Details

#configure(conf, required = true) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/fluent/plugin/fixed_parser.rb', line 204

def configure(conf, required=true)
  format = conf['format']

  if format == nil
    if required
      raise Fluent::ConfigError, "'format' parameter is required"
    else
      return nil
    end
  end

  if format[0] == ?/ && format[format.length-1] == ?/
    # regexp
    begin
      regexp = Regexp.new(format[1..-2])
      if regexp.named_captures.empty?
        raise "No named captures"
      end
    rescue
      raise Fluent::ConfigError, "Invalid regexp '#{format[1..-2]}': #{$!}"
    end
    @parser = RegexpParser.new(regexp)

  else
    # built-in template
    factory = TEMPLATE_FACTORIES[format]
    unless factory
      raise Fluent::ConfigError, "Unknown format template '#{format}'"
    end
    @parser = factory.call

  end

  if @parser.respond_to?(:configure)
    @parser.configure(conf)
  end

  return true
end

#parse(text) ⇒ Object



244
245
246
# File 'lib/fluent/plugin/fixed_parser.rb', line 244

def parse(text)
  return @parser.call(text)
end