Module: WorkerTools::Recorder
- Defined in:
- lib/worker_tools/recorder.rb
Instance Method Summary collapse
- #add_log(message, level = nil) ⇒ Object
- #add_note(message, level = nil) ⇒ Object
- #default_message_attrs(message, level) ⇒ Object
- #error_to_text(error, trace_lines = 20) ⇒ Object
- #format_message(message) ⇒ Object
- #info_error_trace_lines ⇒ Object
- #level_from_message_type(message) ⇒ Object
- #log_directory ⇒ Object
- #log_error_trace_lines ⇒ Object
- #log_file_name ⇒ Object
- #logger ⇒ Object
- #record(message, level = :info) ⇒ Object
- #record_fail(error) ⇒ Object
- #with_wrapper_logger(&block) ⇒ Object
- #with_wrapper_recorder(&block) ⇒ Object
Instance Method Details
#add_log(message, level = nil) ⇒ Object
30 31 32 33 |
# File 'lib/worker_tools/recorder.rb', line 30 def add_log(, level = nil) attrs = (, level) logger.public_send(attrs[:level], (attrs[:message])) end |
#add_note(message, level = nil) ⇒ Object
35 36 37 38 |
# File 'lib/worker_tools/recorder.rb', line 35 def add_note(, level = nil) attrs = (, level) model.notes.push(level: attrs[:level], message: attrs[:message]) end |
#default_message_attrs(message, level) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/worker_tools/recorder.rb', line 57 def (, level) { message: (), level: level || () } end |
#error_to_text(error, trace_lines = 20) ⇒ Object
87 88 89 90 |
# File 'lib/worker_tools/recorder.rb', line 87 def error_to_text(error, trace_lines = 20) txt = "Error: #{error.} (#{error.class})" txt << "Backtrace:\n#{error.backtrace[0, trace_lines].join("\n\t")}" end |
#format_message(message) ⇒ Object
51 52 53 54 55 |
# File 'lib/worker_tools/recorder.rb', line 51 def () return error_to_text(, log_error_trace_lines) if .is_a?(Exception) end |
#info_error_trace_lines ⇒ Object
83 84 85 |
# File 'lib/worker_tools/recorder.rb', line 83 def info_error_trace_lines 20 end |
#level_from_message_type(message) ⇒ Object
45 46 47 48 49 |
# File 'lib/worker_tools/recorder.rb', line 45 def () return :error if .is_a?(Exception) :info end |
#log_directory ⇒ Object
71 72 73 |
# File 'lib/worker_tools/recorder.rb', line 71 def log_directory Rails.root.join('log') end |
#log_error_trace_lines ⇒ Object
79 80 81 |
# File 'lib/worker_tools/recorder.rb', line 79 def log_error_trace_lines 20 end |
#log_file_name ⇒ Object
75 76 77 |
# File 'lib/worker_tools/recorder.rb', line 75 def log_file_name "#{self.class.name.underscore.tr('/', '_')}.log" end |
#logger ⇒ Object
64 65 66 67 68 69 |
# File 'lib/worker_tools/recorder.rb', line 64 def logger @logger ||= begin FileUtils.mkdir_p(log_directory) Logger.new(File.join(log_directory, log_file_name)) end end |
#record(message, level = :info) ⇒ Object
40 41 42 43 |
# File 'lib/worker_tools/recorder.rb', line 40 def record(, level = :info) add_log(, level) add_note(, level) end |
#record_fail(error) ⇒ Object
25 26 27 28 |
# File 'lib/worker_tools/recorder.rb', line 25 def record_fail(error) record(error, :error) model.save!(validate: false) end |
#with_wrapper_logger(&block) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/worker_tools/recorder.rb', line 14 def with_wrapper_logger(&block) block.yield # this time we do want to catch Exception to attempt to handle some of the # critical errors. # rubocop:disable Lint/RescueException rescue Exception => e # rubocop:enable Lint/RescueException add_log(e, :error) raise end |
#with_wrapper_recorder(&block) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/worker_tools/recorder.rb', line 3 def with_wrapper_recorder(&block) block.yield # this time we do want to catch Exception to attempt to handle some of the # critical errors. # rubocop:disable Lint/RescueException rescue Exception => e # rubocop:enable Lint/RescueException record_fail(e) raise end |