class ApiValve::Middleware::Logging::Log

Constants

COMPLETE
INCOMING
NON_STANDARD_REQUEST_HEADERS
REQUEST_HEADERS
REQUEST_PAYLOAD
RESPONSE_HEADERS
RESPONSE_PAYLOAD
URL_PARAMS

Attributes

began_at[RW]
env[RW]
response_headers[RW]
response_payload[RW]
status[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/api_valve/middleware/logging.rb, line 17
def initialize(options = {})
  assign options
end

Public Instance Methods

after_request() click to toggle source
# File lib/api_valve/middleware/logging.rb, line 34
def after_request
  log_response_headers if Logging.log_response_headers
  log_response_payload if Logging.log_response_body
  log_response
end
assign(options = {}) click to toggle source
# File lib/api_valve/middleware/logging.rb, line 21
def assign(options = {})
  options.each do |k, v|
    public_send "#{k}=", v
  end
end
before_request() click to toggle source
# File lib/api_valve/middleware/logging.rb, line 27
def before_request
  log_request
  log_url_params
  log_request_headers if Logging.log_request_headers
  log_request_payload if Logging.log_request_body
end

Private Instance Methods

log_request() click to toggle source
# File lib/api_valve/middleware/logging.rb, line 44
def log_request
  logger.info INCOMING % [
    env['REQUEST_METHOD'],
    [env['PATH_INFO'], env['QUERY_STRING'].presence].compact.join('?'),
    (env['HTTP_X_FORWARDED_FOR'] || env['REMOTE_ADDR']),
    began_at.strftime('%Y-%m-%d %H:%M:%S %z')
  ]
end
log_request_headers() click to toggle source
# File lib/api_valve/middleware/logging.rb, line 59
def log_request_headers
  headers = {}
  env.each_pair do |k, v|
    next unless k =~ /^HTTP_/ && v.present?
    next if v.blank? || (!k.start_with?('HTTP_') && !NON_STANDARD_REQUEST_HEADERS.include?(k))

    headers[k] = v
  end
  return if headers.empty?

  logger.debug REQUEST_HEADERS % headers
end
log_request_payload() click to toggle source
# File lib/api_valve/middleware/logging.rb, line 72
def log_request_payload
  return unless %w(PATCH POST PUT).include? env['REQUEST_METHOD']

  logger.debug REQUEST_PAYLOAD % env['rack.input'].read(1000)
  env['rack.input'].rewind
end
log_response() click to toggle source
# File lib/api_valve/middleware/logging.rb, line 91
def log_response
  logger.info COMPLETE % [
    status,
    Rack::Utils::HTTP_STATUS_CODES[status],
    (Time.now - began_at) * 1000
  ]
end
log_response_headers() click to toggle source
# File lib/api_valve/middleware/logging.rb, line 79
def log_response_headers
  return if response_headers.empty?

  logger.debug RESPONSE_HEADERS % response_headers.inspect
end
log_response_payload() click to toggle source
# File lib/api_valve/middleware/logging.rb, line 85
def log_response_payload
  return if response_payload.empty?

  logger.debug RESPONSE_PAYLOAD % response_payload.first(config_log_body_size)
end
log_url_params() click to toggle source
# File lib/api_valve/middleware/logging.rb, line 53
def log_url_params
  return unless env['QUERY_STRING'].present?

  logger.info URL_PARAMS % Rack::Utils.parse_nested_query(env['QUERY_STRING']).inspect
end