Class: ExceptionNotifier::DbNotifier

Inherits:
BaseNotifier show all
Defined in:
lib/exception_notifier/db_notifier.rb

Instance Attribute Summary

Attributes inherited from BaseNotifier

#base_options

Instance Method Summary collapse

Methods inherited from BaseNotifier

#_post_callback, #_pre_callback, #send_notice

Constructor Details

#initialize(opts = {}) ⇒ DbNotifier

Returns a new instance of DbNotifier.



5
6
7
# File 'lib/exception_notifier/db_notifier.rb', line 5

def initialize(opts = {})
  super(opts)
end

Instance Method Details

#call(exception, opts = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/exception_notifier/db_notifier.rb', line 9

def call(exception, opts = {})
  return unless ExceptionTrack.config.enabled_env?(Rails.env)

  # send the notification
  title = exception.message || "None"
  messages = []

  ActiveSupport::Notifications.instrument("track.exception_track", title: title) do
    messages << headers_for_env(opts[:env])
    messages << ""
    messages << "--------------------------------------------------"
    messages << ""
    messages << exception.inspect
    unless exception.backtrace.blank?
      messages << "\n"
      messages << exception.backtrace
    end

    Rails.logger.silence do
      ExceptionTrack::Log.create(title: title[0, 200], body: messages.join("\n"))
    end
  end
rescue => e
  errs = []
  errs << "-- [ExceptionTrack] create error ---------------------------"
  errs << e.message.indent(2)
  errs << ""
  errs << "-- Exception detail ----------------------------------------"
  errs << title.indent(2)
  errs << ""
  errs << messages.join("\n").indent(2)
  Rails.logger.error errs.join("\n")
end

#filter_parameters(env) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/exception_notifier/db_notifier.rb', line 64

def filter_parameters(env)
  parameters = env["action_dispatch.request.parameters"] || {}
  parameter_filter = ActiveSupport::ParameterFilter.new(env["action_dispatch.parameter_filter"] || [])
  parameter_filter.filter(parameters)
rescue => e
  Rails.logger.error "filter_parameters error: #{e.inspect}"
  parameters
end

#headers_for_env(env) ⇒ Object

Log Request headers from Rack env



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/exception_notifier/db_notifier.rb', line 44

def headers_for_env(env)
  return "" if env.blank?

  parameters = filter_parameters(env)

  headers = []
  headers << "Method:      #{env["REQUEST_METHOD"]}"
  headers << "URL:         #{env["REQUEST_URI"]}"
  headers << "Parameters:\n#{pretty_hash(parameters.except(:controller, :action), 13)}" if env["REQUEST_METHOD"].downcase != "get"
  headers << "Controller:  #{parameters["controller"]}##{parameters["action"]}"
  headers << "RequestId:   #{env["action_dispatch.request_id"]}"
  headers << "User-Agent:  #{env["HTTP_USER_AGENT"]}"
  headers << "Remote IP:   #{env["REMOTE_ADDR"]}"
  headers << "Language:    #{env["HTTP_ACCEPT_LANGUAGE"]}"
  headers << "Server:      #{Socket.gethostname}"
  headers << "Process:     #{$PROCESS_ID}"

  headers.join("\n")
end

#pretty_hash(params, indent = 0) ⇒ Object



73
74
75
76
# File 'lib/exception_notifier/db_notifier.rb', line 73

def pretty_hash(params, indent = 0)
  json = JSON.pretty_generate(params)
  json.indent(indent)
end