Class: Fluent::TextParser

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

Defined Under Namespace

Modules: TypeConverter Classes: ApacheParser, CSVParser, JSONParser, LabeledTSVParser, MultilineParser, NoneParser, RegexpParser, SyslogParser, TSVParser, TimeParser, ValuesParser

Constant Summary collapse

ParserError =

Keep backward compatibility for existing plugins

::Fluent::ParserError
TEMPLATE_REGISTRY =
Registry.new(:config_type, 'fluent/plugin/parser_')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTextParser

Returns a new instance of TextParser.



740
741
742
743
# File 'lib/fluent/parser.rb', line 740

def initialize
  @parser = nil
  @estimate_current_event = nil
end

Instance Attribute Details

#estimate_current_eventObject

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



749
750
751
# File 'lib/fluent/parser.rb', line 749

def estimate_current_event
  @estimate_current_event
end

#parserObject (readonly)

Returns the value of attribute parser.



745
746
747
# File 'lib/fluent/parser.rb', line 745

def parser
  @parser
end

Class Method Details

.lookup(format) ⇒ Object



711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
# File 'lib/fluent/parser.rb', line 711

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

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



698
699
700
701
702
703
704
705
706
707
708
709
# File 'lib/fluent/parser.rb', line 698

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

Instance Method Details

#configure(conf, required = true) ⇒ Object



751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'lib/fluent/parser.rb', line 751

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) ⇒ Object



766
767
768
769
770
771
772
# File 'lib/fluent/parser.rb', line 766

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