Module: HAProxyLogParser
- Defined in:
- lib/haproxy_log_parser.rb,
lib/haproxy_log_parser/entry.rb,
lib/haproxy_log_parser/error_entry.rb
Defined Under Namespace
Classes: Entry, ErrorEntry, ParseError
Constant Summary collapse
- VERSION =
IO.read(File.join(File.dirname(__FILE__), '..', 'VERSION')).chomp.freeze
Class Method Summary collapse
-
.decode_captured_cookie(string) ⇒ Hash{String => String}
Converts a captured cookie string to a Hash.
-
.decode_captured_headers(string) ⇒ Array<String>
Converts a captured headers string to an Array.
-
.parse(line) ⇒ Entry, ErrorEntry
Returns an Entry or ErrorEntry object resulting from the given HAProxy HTTP-format log
line
. -
.parse_accept_date(string) ⇒ Time
Converts the value of an accept_date field to a Time object.
-
.unescape(string) ⇒ String
Returns the given string un-escaped.
Class Method Details
.decode_captured_cookie(string) ⇒ Hash{String => String}
Converts a captured cookie string to a Hash.
67 68 69 70 71 72 73 74 |
# File 'lib/haproxy_log_parser.rb', line 67 def (string) if string == '-' {} else key, value = string.split('=', 2) {unescape(key) => unescape(value)} end end |
.decode_captured_headers(string) ⇒ Array<String>
Converts a captured headers string to an Array.
80 81 82 |
# File 'lib/haproxy_log_parser.rb', line 80 def decode_captured_headers(string) string.split('|', -1).map! { |header| unescape(header) } end |
.parse(line) ⇒ Entry, ErrorEntry
Returns an Entry or ErrorEntry object resulting from the given HAProxy HTTP-format log line
.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/haproxy_log_parser.rb', line 20 def parse(line) parser = LineParser.new result = parser.parse(line) unless result raise ParseError, parser.failure_reason end entry = if result.suffix.respond_to?(:http_request) create_normal_entry(result.suffix) else create_error_entry(result.suffix) end entry.client_ip = result.client_ip.text_value entry.client_port = result.client_port.text_value.to_i entry.accept_date = parse_accept_date(result.accept_date.text_value) entry end |
.parse_accept_date(string) ⇒ Time
Converts the value of an accept_date field to a Time object.
56 57 58 59 |
# File 'lib/haproxy_log_parser.rb', line 56 def parse_accept_date(string) parts = string.split(/[\/:.]/) Time.local(*parts.values_at(2, 1, 0, 3..6)) end |
.unescape(string) ⇒ String
Returns the given string un-escaped. See the “Logging > Non-printable characters” section in HAProxy documentation.
46 47 48 49 50 |
# File 'lib/haproxy_log_parser.rb', line 46 def unescape(string) string.gsub(/#[[:xdigit:]]{2}/) do |match| match[1..-1].to_i(16).chr end end |