Class: ExceptionNotifier::FileNotifier
- Inherits:
-
BaseNotifier
- Object
- BaseNotifier
- ExceptionNotifier::FileNotifier
- Includes:
- BacktraceCleaner
- Defined in:
- lib/exception_notifier/file_notifier.rb
Defined Under Namespace
Classes: MissingController
Instance Method Summary collapse
- #append_log(message) ⇒ Object
- #background_exception_notification(exception, options) ⇒ Object
- #call(exception = nil, options = {}) ⇒ Object
- #deep_encode(obj) ⇒ Object
- #encode_to_utf8(str) ⇒ Object
- #error_message(exception) ⇒ Object
- #exception_message(env, exception, options = {}) ⇒ Object
- #exception_notification(env, exception, options) ⇒ Object
- #generate_json(message) ⇒ Object
-
#initialize(options = {}) ⇒ FileNotifier
constructor
A new instance of FileNotifier.
-
#quick_sanitization(str) ⇒ Object
stringify any random objects in a safe (and convenient) manner.
- #render_notice ⇒ Object
- #set_data_variables ⇒ Object
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(={}) filename = [:filename] || "log/error.log" shift_age = [:shift_age] || 0 shift_size = [:shift_size] || nil = [filename, shift_age, shift_size] @formatter = -> (_, _, _, ) { + "\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() log = ::Logger.new(*) log.formatter = @formatter error_log = generate_json() 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, ) @exception = exception = .symbolize_keys @backtrace = exception.backtrace || [] = ::Time.current @data = [: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, ={}) append_log ([:env], exception, ) 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 (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 (env, exception, ={}) return (exception) unless exception.is_a?(Exception) if env.present? exception_notification([:env], exception, ) else background_exception_notification(exception, ) 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, ) @env = env @exception = exception = .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) : [] = ::Time.current @data = (@env['exception_notifier.exception_data'] || {}).merge([:data] || {}) render_notice end |
#generate_json(message) ⇒ Object
33 34 35 |
# File 'lib/exception_notifier/file_notifier.rb', line 33 def generate_json() ::JSON.generate(deep_encode()) 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_notice ⇒ Object
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., 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_variables ⇒ Object
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 |