class Logeater::Parser

Constants

LINE_MATCHER
REQUEST_COMPLETED_MATCHER
REQUEST_CONTROLLER_MATCHER
REQUEST_LINE_MATCHER
REQUEST_PARAMETERS_MATCHER
REQUEST_STARTED_MATCHER

Attributes

normalized_controller_name[R]
parsed_uri[R]

Public Class Methods

new() click to toggle source
# File lib/logeater/parser.rb, line 132
def initialize
  @normalized_controller_name = Hash.new do |hash, controller_name|
    hash[controller_name] = controller_name.underscore.gsub(/_controller$/, "")
  end

  @parsed_uri = Hash.new do |hash, uri|
    hash[uri] = Addressable::URI.parse(uri).path
  end
end

Public Instance Methods

log(statement) click to toggle source
# File lib/logeater/parser.rb, line 126
def log(statement)
  $stderr.puts "\e[33m#{statement}\e[0m"
end
parse!(line) click to toggle source
# File lib/logeater/parser.rb, line 50
def parse!(line)
  match = line.match LINE_MATCHER
  raise UnmatchedLine.new(line) unless match

  result = {
    type: :generic,
    timestamp: match["timestamp"],
    log_level: match["log_level"],
    message: match["message"]
  }.merge(
    parse_message(match["message"]))
end
parse_message(message) click to toggle source
# File lib/logeater/parser.rb, line 63
def parse_message(message)
  match = message.match REQUEST_LINE_MATCHER
  return {} unless match

  message = match["message"]

  { subdomain: match["subdomain"],
    uuid: match["uuid"],
    type: :request_line,
    user_id: match["user_id"] && match["user_id"].to_i,
    tester_bar: !!match["tester_bar"],
    message: message
  }.merge(
    parse_message_extra(message))
end
parse_message_extra(message) click to toggle source
# File lib/logeater/parser.rb, line 79
def parse_message_extra(message)
  match = message.match(REQUEST_STARTED_MATCHER)
  return parse_request_started_message(match) if match

  match = message.match(REQUEST_CONTROLLER_MATCHER)
  return parse_request_controller_message(match) if match

  match = message.match(REQUEST_PARAMETERS_MATCHER)
  return parse_request_params_message(match) if match

  match = message.match(REQUEST_COMPLETED_MATCHER)
  return parse_request_completed_message(match) if match

  {}
end
parse_request_completed_message(match) click to toggle source
# File lib/logeater/parser.rb, line 117
def parse_request_completed_message(match)
  { type: :request_completed,
    http_status: match["http_status"].to_i,
    http_response: match["http_response"],
    duration: match["duration"].to_i }
end
parse_request_controller_message(match) click to toggle source
# File lib/logeater/parser.rb, line 102
def parse_request_controller_message(match)
  { type: :request_controller,
    controller: normalized_controller_name[match["controller"]],
    action: match["action"],
    format: match["format"] }
end
parse_request_params_message(match) click to toggle source
# File lib/logeater/parser.rb, line 109
def parse_request_params_message(match)
  { type: :request_params,
    params: ParamsParser.new(match["params"]).parse! }
rescue Logeater::Parser::MalformedParameters
  log "Unable to parse parameters: #{match["params"].inspect}"
  { params: match["params"] }
end
parse_request_started_message(match) click to toggle source
# File lib/logeater/parser.rb, line 95
def parse_request_started_message(match)
  { type: :request_started,
    http_method: match["http_method"],
    path: parsed_uri[match["path"]],
    remote_ip: match["remote_ip"] }
end