class Fluent::TextParser

Constants

ParserError

Keep backward compatibility for existing plugins

TEMPLATE_REGISTRY

Attributes

estimate_current_event[RW]

SET false BEFORE CONFIGURE, to return nil when time not parsed 'configure()' may raise errors for unexpected configurations

parser[R]

Public Class Methods

lookup(format) click to toggle source
# File lib/fluent/parser.rb, line 756
def self.lookup(format)
  if format.nil?
    raise ConfigError, "'format' parameter is required"
  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

    RegexpParser.new(regexp)
  else
    # built-in template
    begin
      factory = TEMPLATE_REGISTRY.lookup(format)
    rescue ConfigError => e # keep same error message
      raise ConfigError, "Unknown format template '#{format}'"
    end

    factory.call
  end
end
new() click to toggle source
# File lib/fluent/parser.rb, line 785
def initialize
  @parser = nil
  @estimate_current_event = nil
end
register_template(name, regexp_or_proc, time_format=nil) click to toggle source
# File lib/fluent/parser.rb, line 743
def self.register_template(name, regexp_or_proc, time_format=nil)
  if regexp_or_proc.is_a?(Class)
    factory = Proc.new { regexp_or_proc.new }
  elsif 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_REGISTRY.register(name, factory)
end

Public Instance Methods

configure(conf, required=true) click to toggle source
# File lib/fluent/parser.rb, line 796
def configure(conf, required=true)
  format = conf['format']

  @parser = TextParser.lookup(format)
  if ! @estimate_current_event.nil? && @parser.respond_to?(:'estimate_current_event=')
    @parser.estimate_current_event = @estimate_current_event
  end

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

  return true
end
parse(text, &block) click to toggle source
# File lib/fluent/parser.rb, line 811
def parse(text, &block)
  if block
    @parser.parse(text, &block)
  else # keep backward compatibility. Will be removed at v1
    return @parser.parse(text)
  end
end