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 },
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ TextParser

Returns a new instance of TextParser.



230
231
232
233
# File 'lib/fluent/plugin/fixed_parser.rb', line 230

def initialize(logger)
  @log = logger
  @parser = nil
end

Instance Attribute Details

#logObject

Returns the value of attribute log.



227
228
229
# File 'lib/fluent/plugin/fixed_parser.rb', line 227

def log
  @log
end

#parserObject (readonly)

Returns the value of attribute parser.



228
229
230
# File 'lib/fluent/plugin/fixed_parser.rb', line 228

def parser
  @parser
end

Class Method Details

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



216
217
218
219
220
221
222
223
224
225
# File 'lib/fluent/plugin/fixed_parser.rb', line 216

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



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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
# File 'lib/fluent/plugin/fixed_parser.rb', line 235

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

  @parser.log = @log

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

  return true
end

#parse(text) ⇒ Object



277
278
279
# File 'lib/fluent/plugin/fixed_parser.rb', line 277

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