class Dry::Monitor::Rack::Logger

Constants

FILTERED
PATH_INFO
QUERY_MSG
QUERY_STRING
REMOTE_ADDR
REQUEST_METHOD
START_MSG
STOP_MSG

Attributes

config[R]
logger[R]

Public Class Methods

new(logger, config = self.class.config) click to toggle source
# File lib/dry/monitor/rack/logger.rb, line 26
def initialize(logger, config = self.class.config)
  @logger = logger
  @config = config
end

Public Instance Methods

attach(rack_monitor) click to toggle source
# File lib/dry/monitor/rack/logger.rb, line 31
def attach(rack_monitor)
  rack_monitor.on(:start) { |params| log_start_request(params[:env]) }
  rack_monitor.on(:stop) { |params| log_stop_request(**params) }
  rack_monitor.on(:error) { |event| log_exception(event[:exception]) }
end
filter_backtrace(backtrace) click to toggle source
# File lib/dry/monitor/rack/logger.rb, line 78
def filter_backtrace(backtrace)
  # TODO: what do we want to do with this?
  backtrace.reject { |l| l.include?("gems") }
end
filter_params(params) click to toggle source
# File lib/dry/monitor/rack/logger.rb, line 83
def filter_params(params)
  params.each do |k, v|
    if config.filtered_params.include?(k)
      params[k] = FILTERED
    elsif v.is_a?(Hash)
      filter_params(v)
    elsif v.is_a?(Array)
      v.map! { |m| m.is_a?(Hash) ? filter_params(m) : m }
    end
  end

  params
end
info(*args) click to toggle source
# File lib/dry/monitor/rack/logger.rb, line 68
def info(*args)
  logger.info(*args)
end
log_exception(err) click to toggle source
# File lib/dry/monitor/rack/logger.rb, line 37
def log_exception(err)
  logger.error err.message
  logger.error filter_backtrace(err.backtrace).join("\n")
end
log_request_params(request) click to toggle source
# File lib/dry/monitor/rack/logger.rb, line 62
def log_request_params(request)
  with_http_params(request[QUERY_STRING]) do |params|
    logger.info QUERY_MSG + params.inspect
  end
end
log_start_request(request) click to toggle source
# File lib/dry/monitor/rack/logger.rb, line 42
def log_start_request(request)
  logger.info START_MSG % [
    request[REQUEST_METHOD],
    request[PATH_INFO],
    request[REMOTE_ADDR],
    Time.now
  ]
  log_request_params(request)
end
log_stop_request(env:, status:, time:) click to toggle source
# File lib/dry/monitor/rack/logger.rb, line 52
def log_stop_request(env:, status:, time:)
  logger.info STOP_MSG % [
    env[REQUEST_METHOD],
    env[PATH_INFO],
    env[REMOTE_ADDR],
    time,
    status
  ]
end
with_http_params(params) { |filter_params(params)| ... } click to toggle source
# File lib/dry/monitor/rack/logger.rb, line 72
def with_http_params(params)
  params = ::Rack::Utils.parse_nested_query(params)

  yield(filter_params(params)) unless params.empty?
end