Class: FluentExt::TextParser::ApacheParser

Inherits:
Object
  • Object
show all
Includes:
Fluent::Configurable
Defined in:
lib/fluent/plugin/fixed_parser.rb

Constant Summary collapse

REGEXP =
/^(?<host>[^ ]*) [^ ]* (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)")?$/

Instance Method Summary collapse

Instance Method Details

#call(text) ⇒ Object


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fluent/plugin/fixed_parser.rb', line 141

def call(text)
  m = REGEXP.match(text)
  unless m
    unless @suppress_parse_error_log
      $log.warn "pattern not match: #{text.inspect}"
    end

    return nil, nil
  end

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

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

  time = m['time']
  time = Time.strptime(time, "%d/%b/%Y:%H:%M:%S %z").to_i

  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,
  }

  return time, record
end