Class: LogFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/log2counter/vendor/log_parser.rb

Constant Summary collapse

DIRECTIVES =

Add more format directives here.

{
  # format string char => [:symbol to use, /regex to use when matching against log/]
  'h' => [:ip,           /(?:\d+\.\d+\.\d+\.\d+)|(?:[\w.-]+)/],
  'l' => [:auth,         /.*?/],
  'u' => [:username,     /.*?/],
  't' => [:datetime,     /\[.*?\]/],
  'r' => [:request,      /.*?/],
  'R' => [:request,      /.*?(:?\"|\z)/],
  's' => [:status,       /\d+/],
  'b' => [:bytecount,    /-|\d+/],
  'v' => [:domain,       /.*?/],
  'i' => [:header_lines, /.*?/],
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, format) ⇒ LogFormat

Returns a new instance of LogFormat.



54
55
56
57
# File 'lib/log2counter/vendor/log_parser.rb', line 54

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

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



37
38
39
# File 'lib/log2counter/vendor/log_parser.rb', line 37

def format
  @format
end

#format_regexObject (readonly)

Returns the value of attribute format_regex.



37
38
39
# File 'lib/log2counter/vendor/log_parser.rb', line 37

def format_regex
  @format_regex
end

#format_symbolsObject (readonly)

Returns the value of attribute format_symbols.



37
38
39
# File 'lib/log2counter/vendor/log_parser.rb', line 37

def format_symbols
  @format_symbols
end

#nameObject (readonly)

Returns the value of attribute name.



37
38
39
# File 'lib/log2counter/vendor/log_parser.rb', line 37

def name
  @name
end

Instance Method Details

#parse_format(format) ⇒ Object

The symbols are used to map the log to the env variables. The regex is used when checking what format the log is and to extract data.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/log2counter/vendor/log_parser.rb', line 61

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

  log_format_symbols = []
  format_regex       = ''

  format.scan(format_directive) { |condition, subdirective, directive_char, ignored|
    log_format, match_regex = process_directive(directive_char, subdirective, condition)

    ignored.gsub!(/\s/, '\\s') if ignored

    log_format_symbols << log_format
    format_regex       << "(#{match_regex})#{ignored}"
  }

  @format_symbols = log_format_symbols
  @format_regex   = /\A#{format_regex}/
end

#process_directive(directive_char, subdirective, condition) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/log2counter/vendor/log_parser.rb', line 80

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