Class: Fluent::TextParser::GrokPure

Inherits:
Parser
  • Object
show all
Includes:
TypeConverter
Defined in:
lib/fluent/plugin/parser_grok_pure.rb

Defined Under Namespace

Classes: LogProxy, PatternError

Instance Method Summary collapse

Constructor Details

#initializeGrokPure

Returns a new instance of GrokPure.



21
22
23
24
25
26
27
# File 'lib/fluent/plugin/parser_grok_pure.rb', line 21

def initialize
  super
  @time_parser = TimeParser.new(@time_format)
  @mutex = Mutex.new
  @grok = Grok.new
  @grok.logger = LogProxy.new(log)
end

Instance Method Details

#configure(conf) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fluent/plugin/parser_grok_pure.rb', line 29

def configure(conf)
  super

  if @grok_pattern_path
    Dir["#{@grok_pattern_path}/*"].sort.each do |f|
      @grok.add_patterns_from_file(f)
    end
  end

  begin
    @grok.compile(@grok_pattern, true)
  rescue Grok::PatternError => e
    raise PatternError, e.message
  end
end

#parse(text) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fluent/plugin/parser_grok_pure.rb', line 45

def parse(text)
  time = nil
  record = {}

  matched = @grok.match_and_capture(text) do |k,v|
    if k == @time_key
      time = @mutex.synchronize { @time_parser.parse(v) }
    else
      record[k] = @type_converters.nil? ? v : convert_type(k,v)
    end
  end

  if matched
    time ||= Engine.now if @estimate_current_event
    yield time, record
  else
    yield nil, nil
  end
end