class Fluent::Plugin::MultilineParser

Constants

FORMAT_MAX_NUM

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method Fluent::Plugin::Parser#configure
# File lib/fluent/plugin/parser_multiline.rb, line 30
def configure(conf)
  super

  formats = parse_formats(conf).compact.map { |f| f[1..-2] }.join
  begin
    regexp = Regexp.new(formats, Regexp::MULTILINE)
    if regexp.named_captures.empty?
      raise "No named captures"
    end
    regexp_conf = Fluent::Config::Element.new("", "", { "expression" => "/#{formats}/m" }, [])
    @parser = Fluent::Plugin::RegexpParser.new
    @parser.configure(conf + regexp_conf)
  rescue => e
    raise Fluent::ConfigError, "Invalid regexp '#{formats}': #{e}"
  end

  if @format_firstline
    check_format_regexp(@format_firstline, 'format_firstline')
    @firstline_regex = Regexp.new(@format_firstline[1..-2])
  end
end
firstline?(text) click to toggle source
# File lib/fluent/plugin/parser_multiline.rb, line 60
def firstline?(text)
  @firstline_regex.match(text)
end
has_firstline?() click to toggle source
# File lib/fluent/plugin/parser_multiline.rb, line 56
def has_firstline?
  !!@format_firstline
end
parse(text, &block) click to toggle source
# File lib/fluent/plugin/parser_multiline.rb, line 52
def parse(text, &block)
  @parser.call(text, &block)
end

Private Instance Methods

check_format_range(conf) click to toggle source
# File lib/fluent/plugin/parser_multiline.rb, line 83
def check_format_range(conf)
  invalid_formats = conf.keys.select { |k|
    m = k.match(/^format(\d+)$/)
    m ? !((1..FORMAT_MAX_NUM).include?(m[1].to_i)) : false
  }
  unless invalid_formats.empty?
    raise Fluent::ConfigError, "Invalid formatN found. N should be 1 - #{FORMAT_MAX_NUM}: " + invalid_formats.join(",")
  end
end
check_format_regexp(format, key) click to toggle source
# File lib/fluent/plugin/parser_multiline.rb, line 93
def check_format_regexp(format, key)
  if format[0] == '/' && format[-1] == '/'
    begin
      Regexp.new(format[1..-2], Regexp::MULTILINE)
    rescue => e
      raise Fluent::ConfigError, "Invalid regexp in #{key}: #{e}"
    end
  else
    raise Fluent::ConfigError, "format should be Regexp, need //, in #{key}: '#{format}'"
  end
end
parse_formats(conf) click to toggle source
# File lib/fluent/plugin/parser_multiline.rb, line 66
def parse_formats(conf)
  check_format_range(conf)

  prev_format = nil
  (1..FORMAT_MAX_NUM).map { |i|
    format = conf["format#{i}"]
    if (i > 1) && prev_format.nil? && !format.nil?
      raise Fluent::ConfigError, "Jump of format index found. format#{i - 1} is missing."
    end
    prev_format = format
    next if format.nil?

    check_format_regexp(format, "format#{i}")
    format
  }
end