Module: Nuggets::LogParser::Apache

Extended by:
Apache
Included in:
Apache
Defined in:
lib/nuggets/log_parser/apache.rb

Constant Summary collapse

DEFAULT_RE =
%r{(.*?)}
DIRECTIVES =
{
  'h' => [:ip,        %r{(\d+(?:\.\d+){3}|[\w.-]+)}],
  'l' => [:auth,      DEFAULT_RE],
  'u' => [:username,  DEFAULT_RE],
  't' => [:datetime,  %r{\[(.*?)\]}],
  'r' => [:request,   DEFAULT_RE],
  'R' => [:request,   %r{(.*?)(?:"|\z)}],
  's' => [:status,    %r{(\d+)}],
  'b' => [:bytecount, %r{(-|\d+)}],
  'v' => [:domain,    DEFAULT_RE],
  'i' => [nil,        DEFAULT_RE],
}
DIRECTIVES_RE =
%r{%.*?(?:\{(.*?)\})?([#{DIRECTIVES.keys.join}])([\s\\"]*)}
ORDER =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.detect_type(line) ⇒ Object



79
80
81
# File 'lib/nuggets/log_parser/apache.rb', line 79

def detect_type(line)
  ORDER.find { |type| line =~ type::RE }
end

.parse_format(format) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/nuggets/log_parser/apache.rb', line 67

def parse_format(format)
  re, items = '\A', []

  format.scan(DIRECTIVES_RE) { |h, c, e|
    i, r = DIRECTIVES[c]
    re << r.source << e.gsub(/\s/, '\\s')
    items << i ||= h.downcase.tr('-', '_').to_sym
  }

  [Regexp.new(re), items]
end

.register(name, format) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/nuggets/log_parser/apache.rb', line 56

def register(name, format)
  base = const_set(name, Module.new)

  re, items = parse_format(format)
  base.const_set(:RE,    re)
  base.const_set(:ITEMS, items)

  ORDER << base
  LogParser.register(base, self)
end

Instance Method Details

#parse_line(line, entry = {}) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/nuggets/log_parser/apache.rb', line 90

def parse_line(line, entry = {})
  if md = self::RE.match(line)
    self::ITEMS.each_with_index { |k, i| entry[k] = md[i + 1] }
    yield if block_given?
  end

  entry
end