Class: Fluent::Plugin::RegexpParser

Inherits:
Parser show all
Includes:
Compat::TypeConverter
Defined in:
lib/fluent/plugin/parser_regexp.rb

Constant Summary

Constants included from Compat::TypeConverter

Compat::TypeConverter::Converters

Constants included from Configurable

Configurable::CONFIG_TYPE_REGISTRY

Instance Attribute Summary

Attributes inherited from Parser

#estimate_current_event

Instance Method Summary collapse

Methods included from Compat::TypeConverter

included

Methods inherited from Parser

#call

Methods included from OwnedByMixin

#log, #owner, #owner=

Methods inherited from Base

#after_shutdown, #after_shutdown?, #after_start, #after_started?, #before_shutdown, #before_shutdown?, #close, #closed?, #configured?, #has_router?, #inspect, #shutdown, #shutdown?, #start, #started?, #stop, #stopped?, #terminate, #terminated?

Methods included from SystemConfig::Mixin

#system_config, #system_config_override

Methods included from Configurable

#config, included, lookup_type, register_type

Constructor Details

#initializeRegexpParser

Returns a new instance of RegexpParser.



14
15
16
17
# File 'lib/fluent/plugin/parser_regexp.rb', line 14

def initialize
  super
  @mutex = Mutex.new
end

Instance Method Details

#configure(conf) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fluent/plugin/parser_regexp.rb', line 19

def configure(conf)
  super
  @time_parser = TimeParser.new(@time_format)
  unless @expression.empty?
    if @expression[0] == "/" && @expression[-1] == "/"
      regexp_option = 0
      regexp_option |= Regexp::IGNORECASE if @ignorecase
      regexp_option |= Regexp::MULTILINE if @multiline
      @regexp = Regexp.new(@expression[1..-2], regexp_option)
    else
      raise Fluent::ConfigError, "expression must start with `/` and end with `/`: #{@expression}"
    end
  end
end

#parse(text) {|time, record| ... } ⇒ Object

Yields:

  • (time, record)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fluent/plugin/parser_regexp.rb', line 34

def parse(text)
  m = @regexp.match(text)
  unless m
    yield nil, nil
    return
  end

  time = nil
  record = {}

  m.names.each do |name|
    if value = m[name]
      if name == @time_key
        time = @mutex.synchronize { @time_parser.parse(value) }
        if @keep_time_key
          record[name] = if @type_converters.nil?
                           value
                         else
                           convert_type(name, value)
                         end
        end
      else
        record[name] = if @type_converters.nil?
                         value
                       else
                         convert_type(name, value)
                       end
      end
    end
  end

  if @estimate_current_event
    time ||= Fluent::EventTime.now
  end

  yield time, record
end