Class: Fluent::Compat::TextParser

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

Defined Under Namespace

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

Constant Summary collapse

ParserError =

Keep backward compatibility for existing plugins

Fluent::Plugin::Parser::ParserError
TypeConverter =

TODO: will be removed at v1

Fluent::TypeConverter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTextParser

Returns a new instance of TextParser.



46
47
48
49
50
# File 'lib/fluent/compat/parser.rb', line 46

def initialize
  # TODO: warn when deprecated
  @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



56
57
58
# File 'lib/fluent/compat/parser.rb', line 56

def estimate_current_event
  @estimate_current_event
end

#parserObject (readonly)

Returns the value of attribute parser.



52
53
54
# File 'lib/fluent/compat/parser.rb', line 52

def parser
  @parser
end

Class Method Details

.lookup(format) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fluent/compat/parser.rb', line 95

def self.lookup(format)
  # TODO: warn when deprecated to use Plugin.new_parser or RegexpParser.new directly
  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
      Fluent::Plugin.new_parser(format)
    rescue ConfigError # keep same error message
      raise ConfigError, "Unknown format template '#{format}'"
    end
  end
end

.register_template(type, template, time_format = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/fluent/compat/parser.rb', line 84

def self.register_template(type, template, time_format=nil)
  # TODO: warn when deprecated to use Plugin.register_parser directly
  if template.is_a?(Class) || template.respond_to?(:call)
    Fluent::Plugin.register_parser(type, template)
  elsif template.is_a?(Regexp)
    Fluent::Plugin.register_parser(type, Proc.new { RegexpParser.new(template, {'time_format' => time_format}) })
  else
    raise ArgumentError, "Template for parser must be a Class, callable object or regular expression object"
  end
end

Instance Method Details

#configure(conf, required = true) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fluent/compat/parser.rb', line 58

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

  @parser = TextParser.lookup(format)

  if @parser.respond_to?(:configure)
    @parser.configure(conf)
  end
  if !@estimate_current_event.nil? && @parser.respond_to?(:'estimate_current_event=')
    # external code sets parser.estimate_current_event = false
    @parser.estimate_current_event = @estimate_current_event
  end

  return true
end

#parse(text, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/fluent/compat/parser.rb', line 74

def parse(text, &block)
  if block
    @parser.parse(text, &block)
  else
    @parser.parse(text) { |time, record|
      return time, record
    }
  end
end