Module: App47Logger
- Included in:
- ApplicationJob, EmailAble, ExceptionsController, StandardModel
- Defined in:
- lib/app/models/concerns/app47_logger.rb
Overview
Mixin for App47 objects to handle logging in both the rails environment as well as the delayed jobs environment that doesn’t know about Rails.logger
Provides ways to log at the class level as well as instance level.
Usage:
App47Logger.log_debug('message')
or
class ClassName
include App47Logger
def method_name
log_debug('message')
end
Class Method Summary collapse
-
.clean_params(pars) ⇒ Object
Recursively hide any fields in the rails config filter_parameters.
-
.delete_parameter_keys ⇒ Object
which parameters to filter out.
-
.log_debug(message) ⇒ Object
Log a debug messages.
-
.log_error(message, exception = nil) ⇒ Object
Log an error messages.
-
.log_exception(level, exception = nil, max_frame = 9) ⇒ Object
Log a given exception, but only the first 10 lines in anything but test.
-
.log_message(level, message) ⇒ Object
Log a given message at the given level.
-
.log_warn(message, exception = nil) ⇒ Object
Log a warning messages.
-
.mask_parameter_keys ⇒ Object
which parameters to filter out.
Instance Method Summary collapse
- #clean_params(pars) ⇒ Object
-
#log_controller_error(exception, redirecting = false) ⇒ Object
Log a controller error message.
-
#log_debug(message) ⇒ Object
Log a debug message as part of the mixin.
-
#log_error(message, exception = nil) ⇒ Object
Log an error message as part of the mixin.
-
#log_message(level, message) ⇒ Object
Log a debug message as part of the mixin.
-
#log_warn(message, exception = nil) ⇒ Object
Log a warning message as part of the mixin.
-
#update_flash_messages(exception, redirecting = false) ⇒ Object
Update the flash message based on the type of exception we get Currently we only handle mongoid errors, but there may be other common errors we can handle formatting for…
Class Method Details
.clean_params(pars) ⇒ Object
Recursively hide any fields in the rails config filter_parameters
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/app/models/concerns/app47_logger.rb', line 153 def self.clean_params(pars) pars.each do |key, value| if App47Logger.mask_parameter_keys.include?(key.to_s) pars[key] = '[FILTERED]' elsif App47Logger.delete_parameter_keys.include?(key.to_s) pars.delete(key) elsif value.is_a?(Hash) || value.is_a?(ActionController::Parameters) pars[key] = clean_params(value) end end pars end |
.delete_parameter_keys ⇒ Object
which parameters to filter out
176 177 178 |
# File 'lib/app/models/concerns/app47_logger.rb', line 176 def self.delete_parameter_keys @delete_parameter_keys ||= %w[file authenticity_token commit upload] end |
.log_debug(message) ⇒ Object
Log a debug messages
24 25 26 |
# File 'lib/app/models/concerns/app47_logger.rb', line 24 def self.log_debug() :debug, end |
.log_error(message, exception = nil) ⇒ Object
Log an error messages
-
Prints the messages
-
If an exception is passed n
2a prints the exception message
2b prints the stack trace
49 50 51 52 53 54 |
# File 'lib/app/models/concerns/app47_logger.rb', line 49 def self.log_error(, exception = nil) :error, log_exception(:error, exception) = { message: , exception: exception&. } SlackNotification.say , template: :error_message end |
.log_exception(level, exception = nil, max_frame = 9) ⇒ Object
Log a given exception, but only the first 10 lines in anything but test.
In testing, we want the full stack to know the test case that failed.
61 62 63 64 65 66 67 |
# File 'lib/app/models/concerns/app47_logger.rb', line 61 def self.log_exception(level, exception = nil, max_frame = 9) return if exception.blank? max_frame = -1 if ENV['RACK_ENV'].eql?('test') (level, exception.) exception.backtrace[0..max_frame].each { |frame| (level, frame) } if exception.backtrace.present? end |
.log_message(level, message) ⇒ Object
Log a given message at the given level
72 73 74 75 76 77 78 79 80 |
# File 'lib/app/models/concerns/app47_logger.rb', line 72 def self.(level, ) if Rails.env.test? puts "#{level}: #{}" elsif Delayed::Worker.logger.nil? Rails.logger.send level, else Delayed::Worker.logger.send level, end end |
.log_warn(message, exception = nil) ⇒ Object
Log a warning messages
-
Prints the messages
-
If an exception is passed n
2a prints the exception message
2b prints the stack trace
36 37 38 39 |
# File 'lib/app/models/concerns/app47_logger.rb', line 36 def self.log_warn(, exception = nil) :warn, log_exception :warn, exception end |
.mask_parameter_keys ⇒ Object
which parameters to filter out
169 170 171 |
# File 'lib/app/models/concerns/app47_logger.rb', line 169 def self.mask_parameter_keys @mask_parameter_keys ||= Rails.application.config.filter_parameters end |
Instance Method Details
#clean_params(pars) ⇒ Object
146 147 148 |
# File 'lib/app/models/concerns/app47_logger.rb', line 146 def clean_params(pars) App47Logger.clean_params(pars) end |
#log_controller_error(exception, redirecting = false) ⇒ Object
Log a controller error message
113 114 115 116 117 118 119 120 |
# File 'lib/app/models/concerns/app47_logger.rb', line 113 def log_controller_error(exception, redirecting = false) if exception.is_a? ActionController::RoutingError App47Logger.log_warn "#{controller_name}##{action_name} #{clean_params(params)}", exception else App47Logger.log_error "#{controller_name}##{action_name} #{clean_params(params)}", exception end (exception, redirecting) if defined?(request) && request.present? end |
#log_debug(message) ⇒ Object
Log a debug message as part of the mixin
92 93 94 |
# File 'lib/app/models/concerns/app47_logger.rb', line 92 def log_debug() App47Logger.log_debug end |
#log_error(message, exception = nil) ⇒ Object
Log an error message as part of the mixin
106 107 108 |
# File 'lib/app/models/concerns/app47_logger.rb', line 106 def log_error(, exception = nil) App47Logger.log_error , exception end |
#log_message(level, message) ⇒ Object
Log a debug message as part of the mixin
85 86 87 |
# File 'lib/app/models/concerns/app47_logger.rb', line 85 def (level, ) App47Logger. level, end |
#log_warn(message, exception = nil) ⇒ Object
Log a warning message as part of the mixin
99 100 101 |
# File 'lib/app/models/concerns/app47_logger.rb', line 99 def log_warn(, exception = nil) App47Logger.log_warn , exception end |
#update_flash_messages(exception, redirecting = false) ⇒ Object
Update the flash message based on the type of exception we get Currently we only handle mongoid errors, but there may be other common errors we can handle formatting for…
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/app/models/concerns/app47_logger.rb', line 127 def (exception, redirecting = false) case exception when Mongoid::Errors::Validations if redirecting flash[:error] = exception.problem flash[:warning] = exception.summary else flash.now[:error] = exception.problem flash.now[:warning] = exception.summary end else if redirecting flash[:error] = exception. else flash.now[:error] = exception. end end end |