lib/dry/monitor/rack/logger.rb in dry-monitor-0.3.0 vs lib/dry/monitor/rack/logger.rb in dry-monitor-0.3.1

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'dry/configurable' require 'dry/monitor/rack/middleware' module Dry module Monitor @@ -7,19 +9,19 @@ class Logger extend Dry::Configurable setting :filtered_params, %w[_csrf password] - REQUEST_METHOD = 'REQUEST_METHOD'.freeze - PATH_INFO = 'PATH_INFO'.freeze - REMOTE_ADDR = 'REMOTE_ADDR'.freeze - QUERY_STRING = 'QUERY_STRING'.freeze + REQUEST_METHOD = 'REQUEST_METHOD' + PATH_INFO = 'PATH_INFO' + REMOTE_ADDR = 'REMOTE_ADDR' + QUERY_STRING = 'QUERY_STRING' - START_MSG = %(Started %s "%s" for %s at %s).freeze - STOP_MSG = %(Finished %s "%s" for %s in %sms [Status: %s]\n).freeze - QUERY_MSG = %( Query parameters %s).freeze - FILTERED = '[FILTERED]'.freeze + START_MSG = %(Started %s "%s" for %s at %s) + STOP_MSG = %(Finished %s "%s" for %s in %sms [Status: %s]\n) + QUERY_MSG = %( Query parameters %s) + FILTERED = '[FILTERED]' attr_reader :logger attr_reader :config @@ -40,33 +42,33 @@ rack_monitor.on(:error) do |event| log_exception(event[:exception]) end end - def log_exception(e) - logger.error e.message - logger.error filter_backtrace(e.backtrace).join("\n") + def log_exception(err) + logger.error err.message + logger.error filter_backtrace(err.backtrace).join("\n") end def log_start_request(request) info START_MSG % [ - request[REQUEST_METHOD], - request[PATH_INFO], - request[REMOTE_ADDR], - Time.now - ] + request[REQUEST_METHOD], + request[PATH_INFO], + request[REMOTE_ADDR], + Time.now + ] log_request_params(request) end def log_stop_request(request, status, time) info STOP_MSG % [ - request[REQUEST_METHOD], - request[PATH_INFO], - request[REMOTE_ADDR], - time, - status - ] + request[REQUEST_METHOD], + request[PATH_INFO], + request[REMOTE_ADDR], + time, + status + ] end def log_request_params(request) with_http_params(request[QUERY_STRING]) do |params| info QUERY_MSG % [params.inspect] @@ -78,12 +80,10 @@ end def with_http_params(params) params = ::Rack::Utils.parse_nested_query(params) - if params.size > 0 - yield(filter_params(params)) - end + yield(filter_params(params)) unless params.empty? end def filter_backtrace(backtrace) # TODO: what do we want to do with this? backtrace.reject { |l| l.include?('gems') }