Class: Fluent::Plugin::Apache2Parser

Inherits:
Parser
  • Object
show all
Defined in:
lib/fluent/plugin/parser_apache2.rb

Direct Known Subclasses

Compat::TextParser::ApacheParser

Constant Summary collapse

REGEXP =
/^(?<host>[^ ]*) [^ ]* (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>(?:[^\"]|\\")*?)(?: +\S*)?)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>(?:[^\"]|\\")*)" "(?<agent>(?:[^\"]|\\")*)")?$/
TIME_FORMAT =
"%d/%b/%Y:%H:%M:%S %z"

Constants inherited from Parser

Parser::AVAILABLE_PARSER_VALUE_TYPES, Parser::PARSER_TYPES, Parser::TRUTHY_VALUES

Constants included from Configurable

Configurable::CONFIG_TYPE_REGISTRY

Instance Attribute Summary

Attributes inherited from Parser

#type_converters

Attributes inherited from Base

#under_plugin_development

Instance Method Summary collapse

Methods inherited from Parser

#build_type_converters, #call, #convert_values, #implement?, #parse_io, #parse_partial_data, #parse_time, #parse_with_timeout, #parser_type, #start, #stop, #string_like_null

Methods included from TimeMixin::Parser

included, #time_parser_create

Methods included from OwnedByMixin

#log, #owner, #owner=

Methods inherited from Base

#acquire_worker_lock, #after_shutdown, #after_shutdown?, #after_start, #after_started?, #before_shutdown, #before_shutdown?, #called_in_test?, #close, #closed?, #configured?, #context_router, #context_router=, #fluentd_worker_id, #get_lock_path, #has_router?, #inspect, #multi_workers_ready?, #plugin_root_dir, #reloadable_plugin?, #shutdown, #shutdown?, #start, #started?, #stop, #stopped?, #string_safe_encoding, #terminate, #terminated?

Methods included from SystemConfig::Mixin

#system_config, #system_config_override

Methods included from Configurable

#config, #configure_proxy_generate, #configured_section_create, included, lookup_type, register_type

Constructor Details

#initializeApache2Parser

Returns a new instance of Apache2Parser.



27
28
29
30
# File 'lib/fluent/plugin/parser_apache2.rb', line 27

def initialize
  super
  @mutex = Mutex.new
end

Instance Method Details

#configure(conf) ⇒ Object



32
33
34
35
# File 'lib/fluent/plugin/parser_apache2.rb', line 32

def configure(conf)
  super
  @time_parser = time_parser_create(format: TIME_FORMAT)
end

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

Yields:

  • (time, record)


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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fluent/plugin/parser_apache2.rb', line 41

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

  host = m['host']
  host = (host == '-') ? nil : host

  user = m['user']
  user = (user == '-') ? nil : user

  time = m['time']
  time = @mutex.synchronize { @time_parser.parse(time) }

  method = m['method']
  path = m['path']

  code = m['code'].to_i
  code = nil if code == 0

  size = m['size']
  size = (size == '-') ? nil : size.to_i

  referer = m['referer']
  referer = (referer == '-') ? nil : referer

  agent = m['agent']
  agent = (agent == '-') ? nil : agent

  record = {
    "host" => host,
    "user" => user,
    "method" => method,
    "path" => path,
    "code" => code,
    "size" => size,
    "referer" => referer,
    "agent" => agent,
  }
  record["time"] = m['time'] if @keep_time_key

  yield time, record
end

#patternsObject



37
38
39
# File 'lib/fluent/plugin/parser_apache2.rb', line 37

def patterns
  {'format' => REGEXP, 'time_format' => TIME_FORMAT}
end