Class: HttpLogFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/http/parser.rb

Constant Summary collapse

DIRECTIVES =
{
  'h' => [:ip, /\d+\.\d+\.\d+\.\d+/],
  'l' => [:auth, /.*?/],
  'u' => [:username, /.*?/],
  't' => [:datetime, /\[.*?\]/],
  'r' => [:request, /.*?/],
  's' => [:status, /\d+/],
  'b' => [:bytecount, /-|\d+/],
  'v' => [:domain, /.*?/],
  'i' => [:header_lines, /.*?/],
  'e' => [:errorlevel, /\[.*?\]/],
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, format) ⇒ HttpLogFormat

Returns a new instance of HttpLogFormat.



17
18
19
20
# File 'lib/http/parser.rb', line 17

def initialize(name, format)
  @name, @format = name, format
  parse_format(format)
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



2
3
4
# File 'lib/http/parser.rb', line 2

def format
  @format
end

#format_regexObject (readonly)

Returns the value of attribute format_regex.



2
3
4
# File 'lib/http/parser.rb', line 2

def format_regex
  @format_regex
end

#format_symbolsObject (readonly)

Returns the value of attribute format_symbols.



2
3
4
# File 'lib/http/parser.rb', line 2

def format_symbols
  @format_symbols
end

#nameObject (readonly)

Returns the value of attribute name.



2
3
4
# File 'lib/http/parser.rb', line 2

def name
  @name
end

Instance Method Details

#parse_format(format) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/http/parser.rb', line 22

def parse_format(format)
  format_directive = /%(.*?)(\{.*?\})?([#{[DIRECTIVES.keys.join('|')]}])([\s\\"]*)/

  log_format_symbols = []
  format_regex = ""
  format.scan(format_directive) do |condition, subdirective, directive_char, ignored|
    log_format, match_regex = process_directive(directive_char, subdirective, condition)
    ignored.gsub!(/\s/, '\\s') unless ignored.nil?
    log_format_symbols << log_format
    format_regex << "(#{match_regex})#{ignored}"
  end
  @format_symbols = log_format_symbols
  @format_regex =  /^#{format_regex}/
end

#process_directive(directive_char, subdirective, condition) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/http/parser.rb', line 37

def process_directive(directive_char, subdirective, condition)
  directive = DIRECTIVES[directive_char]
  case directive_char
  when 'i'
    log_format = subdirective[1...-1].downcase.tr('-', '_').to_sym
    [log_format, directive[1].source]
  else
    [directive[0], directive[1].source]
  end
end