Sha256: f8c9a1d4961a17870626862b4af30188be4c57c730ec6d54002977d813fdc210

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

require "fluent/plugin/grok"

module Fluent
  class TextParser
    class GrokPatternNotFoundError < Exception; end

    class GrokParser < Parser
      Plugin.register_parser('grok', self)
      config_param :time_format, :string, :default => nil
      config_param :grok_pattern, :string, :default => nil
      config_param :custom_pattern_path, :string, :default => nil

      def initialize
        super
        @default_parser = NoneParser.new
      end

      def configure(conf={})
        super

        @grok = Grok.new(self, conf)

        default_pattern_dir = File.expand_path('../../../../patterns/*', __FILE__)
        Dir.glob(default_pattern_dir) do |pattern_file_path|
          @grok.add_patterns_from_file(pattern_file_path)
        end

        if @custom_pattern_path
          if Dir.exists? @custom_pattern_path
            Dir.glob(@custom_pattern_path + '/*') do |pattern_file_path|
              @grok.add_patterns_from_file(pattern_file_path)
            end
          elsif File.exists? @custom_pattern_path
            @grok.add_patterns_from_file(@custom_pattern_path)
          end
        end

        @grok.setup
      end

      def parse(text, &block)
        if block_given?
          @grok.parsers.each do |parser|
            parser.parse(text) do |time, record|
              if time and record
                yield time, record
                return
              end
            end
          end
          yield @default_parser.parse(text)
        else
          @grok.parsers.each do |parser|
            parser.parse(text) do |time, record|
              if time and record
                return time, record
              end
            end
          end
          return @default_parser.parse(text)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fluent-plugin-grok-parser-0.0.4 lib/fluent/plugin/parser_grok.rb
fluent-plugin-grok-parser-0.0.3 lib/fluent/plugin/parser_grok.rb