Class: Dry::Monitor::Rack::Logger

Inherits:
Object
  • Object
show all
Extended by:
Configurable
Defined in:
lib/dry/monitor/rack/logger.rb

Constant Summary collapse

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)
STOP_MSG =
%(Finished %s "%s" for %s in %s [Status: %s]\n)
QUERY_MSG =
%(  Query parameters )
FILTERED =
"[FILTERED]"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, config = self.class.config) ⇒ Logger

Returns a new instance of Logger.



25
26
27
28
# File 'lib/dry/monitor/rack/logger.rb', line 25

def initialize(logger, config = self.class.config)
  @logger = logger
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



23
24
25
# File 'lib/dry/monitor/rack/logger.rb', line 23

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



23
24
25
# File 'lib/dry/monitor/rack/logger.rb', line 23

def logger
  @logger
end

Instance Method Details

#attach(rack_monitor) ⇒ Object



30
31
32
33
34
# File 'lib/dry/monitor/rack/logger.rb', line 30

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) ⇒ Object



77
78
79
80
# File 'lib/dry/monitor/rack/logger.rb', line 77

def filter_backtrace(backtrace)
  # TODO: what do we want to do with this?
  backtrace.reject { |l| l.include?("gems") }
end

#filter_params(params) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dry/monitor/rack/logger.rb', line 82

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) ⇒ Object



67
68
69
# File 'lib/dry/monitor/rack/logger.rb', line 67

def info(*args)
  logger.info(*args)
end

#log_exception(err) ⇒ Object



36
37
38
39
# File 'lib/dry/monitor/rack/logger.rb', line 36

def log_exception(err)
  logger.error err.message
  logger.error filter_backtrace(err.backtrace).join("\n")
end

#log_request_params(request) ⇒ Object



61
62
63
64
65
# File 'lib/dry/monitor/rack/logger.rb', line 61

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) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/dry/monitor/rack/logger.rb', line 41

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:) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/dry/monitor/rack/logger.rb', line 51

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)| ... } ⇒ Object

Yields:



71
72
73
74
75
# File 'lib/dry/monitor/rack/logger.rb', line 71

def with_http_params(params)
  params = ::Rack::Utils.parse_nested_query(params)

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