Class: ExceptionNotifier::FileNotifier

Inherits:
BaseNotifier
  • Object
show all
Includes:
BacktraceCleaner
Defined in:
lib/exception_notifier/file_notifier.rb

Defined Under Namespace

Classes: MissingController

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FileNotifier

Returns a new instance of FileNotifier.



12
13
14
15
16
17
18
19
# File 'lib/exception_notifier/file_notifier.rb', line 12

def initialize(options={})
  filename     = options[:filename]   || "log/error.log"
  shift_age    = options[:shift_age]  || 0
  shift_size   = options[:shift_size] || nil

  @logger_options     = [filename, shift_age, shift_size]
  @formatter          = -> (_, _, _, message) { message + "\n" }
end

Instance Method Details

#append_log(message) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/exception_notifier/file_notifier.rb', line 25

def append_log(message)
  log = ::Logger.new(*@logger_options)
  log.formatter = @formatter
  error_log = generate_json(message)
  log.error(error_log)
  log.close
end

#background_exception_notification(exception, options) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/exception_notifier/file_notifier.rb', line 60

def background_exception_notification(exception, options)
  @exception = exception
  @options   = options.symbolize_keys
  @backtrace = exception.backtrace || []
  @timestamp = ::Time.current
  @data      = options[:data] || {}
  @env = @kontroller = nil

  render_notice
end

#call(exception = nil, options = {}) ⇒ Object



21
22
23
# File 'lib/exception_notifier/file_notifier.rb', line 21

def call(exception=nil, options={})
  append_log exception_message(options[:env], exception, options)
end

#deep_encode(obj) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/exception_notifier/file_notifier.rb', line 134

def deep_encode(obj)
  case obj
  when Array
    obj.map { |o| deep_encode(o) }
  when Hash
    deep_encode(obj.to_a).to_h
  when String
    encode_to_utf8(obj)
  else
    obj.respond_to?(:to_s) ? encode_to_utf8(obj.to_s) : '[NOT ENCODABLE]'
  end
end

#encode_to_utf8(str) ⇒ Object



147
148
149
# File 'lib/exception_notifier/file_notifier.rb', line 147

def encode_to_utf8(str)
  quick_sanitization(str).encode(::Encoding.find('UTF-8'), invalid: :replace, undef: :replace, replace: '?')
end

#error_message(exception) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/exception_notifier/file_notifier.rb', line 71

def error_message(exception)
  {
    message:           '[FATAL] NO EXCEPTION GIVEN: Please provide Exception as argument',
    exception:         exception.try(:to_s),
    timestamp:         ::Time.current,
  }
end

#exception_message(env, exception, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/exception_notifier/file_notifier.rb', line 37

def exception_message(env, exception, options={})
  return error_message(exception) unless exception.is_a?(Exception)

  if env.present?
    exception_notification(options[:env], exception, options)
  else
    background_exception_notification(exception, options)
  end
end

#exception_notification(env, exception, options) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/exception_notifier/file_notifier.rb', line 47

def exception_notification(env, exception, options)
  @env        = env
  @exception  = exception
  @options    = options.reverse_merge(@env['exception_notifier.options'] || {}).symbolize_keys
  @kontroller = @env['action_controller.instance'] || MissingController.new
  @request    = ::ActionDispatch::Request.new(@env)
  @backtrace  = exception.backtrace ? clean_backtrace(exception) : []
  @timestamp  = ::Time.current
  @data       = (@env['exception_notifier.exception_data'] || {}).merge(options[:data] || {})

  render_notice
end

#generate_json(message) ⇒ Object



33
34
35
# File 'lib/exception_notifier/file_notifier.rb', line 33

def generate_json(message)
  ::JSON.generate(deep_encode(message))
end

#quick_sanitization(str) ⇒ Object

stringify any random objects in a safe (and convenient) manner



151
152
153
# File 'lib/exception_notifier/file_notifier.rb', line 151

def quick_sanitization(str) # stringify any random objects in a safe (and convenient) manner
  str.inspect.gsub(/\A\"(.*)\"\z/,'\1')
end

#render_noticeObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/exception_notifier/file_notifier.rb', line 79

def render_notice
  set_data_variables

  exception_details = {
    exception:         @exception.class,
    controller:        @kontroller.present? ? @kontroller.controller_name : '[BACKGROUND]',
    action:            @kontroller.present? ? @kontroller.action_name : '[BACKGROUND]',
    exception_message: @exception.message,

    timestamp:         @timestamp,
    server:            ::Socket.gethostname,
    rails_root:        (defined?(::Rails) && ::Rails.respond_to?(:root) ) ? ::Rails.root : nil,
    process:           $$,
    backtrace:         @backtrace,
  }

  if @env.present?
    exception_details.merge!({
      url:               @request.url,
      http_method:       @request.request_method,
      ip_address:        @request.remote_ip,
      parameters:        (@request.filtered_parameters.inspect rescue "[NOT ENCODABLE]"),
      session_id:        (@request.ssl? ? "[FILTERED]" : @request.session['session_id'] || (@request.env["rack.session.options"] and @request.env["rack.session.options"][:id])),
      session_data:      @request.session.to_hash,
    })
    filtered_env = @request.filtered_env
    filtered_env.sort_by { |key, _| key.to_s }.each do |key, value|
      exception_details[key] = value
    end
  end

  exception_details.merge!({
    data:              @data,
  })

  exception_details
end

#set_data_variablesObject



128
129
130
131
132
# File 'lib/exception_notifier/file_notifier.rb', line 128

def set_data_variables
  @data.each do |name, value|
    instance_variable_set("@#{name}", value)
  end
end