Class: Fluent::TextParser

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

Defined Under Namespace

Classes: ApacheParser, CSVParser, 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 },
  '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 },
  'tsv' => Proc.new { TSVParser.new },
  'ltsv' => Proc.new { LabeledTSVParser.new },
  'csv' => Proc.new { CSVParser.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"}) },
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTextParser

Returns a new instance of TextParser.



246
247
248
# File 'lib/fluent/parser.rb', line 246

def initialize
  @parser = nil
end

Class Method Details

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



235
236
237
238
239
240
241
242
243
244
# File 'lib/fluent/parser.rb', line 235

def self.register_template(name, regexp_or_proc, time_format=nil)
  if regexp_or_proc.is_a?(Regexp)
    regexp = regexp_or_proc
    factory = Proc.new { RegexpParser.new(regexp, {'time_format'=>time_format}) }
  else
    factory = regexp_or_proc
  end

  TEMPLATE_FACTORIES[name] = factory
end

Instance Method Details

#configure(conf, required = true) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/fluent/parser.rb', line 250

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

  if format == nil
    if required
      raise 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 ConfigError, "Invalid regexp '#{format[1..-2]}': #{$!}"
    end

    @parser = RegexpParser.new(regexp)

  else
    # built-in template
    factory = TEMPLATE_FACTORIES[format]
    unless factory
      raise 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



290
291
292
# File 'lib/fluent/parser.rb', line 290

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