Module: WorkerTools::Recorder

Defined in:
lib/worker_tools/recorder.rb

Instance Method Summary collapse

Instance Method Details

#add_info(message) ⇒ Object



36
37
38
39
# File 'lib/worker_tools/recorder.rb', line 36

def add_info(message)
  @information ||= ''
  information << "#{format_info_message(message)}\n"
end

#add_log(message, level = :info) ⇒ Object



32
33
34
# File 'lib/worker_tools/recorder.rb', line 32

def add_log(message, level = :info)
  logger.public_send(level, format_log_message(message))
end

#error_to_text(error, trace_lines = 20) ⇒ Object



78
79
80
81
# File 'lib/worker_tools/recorder.rb', line 78

def error_to_text(error, trace_lines = 20)
  txt = "Error: #{error.message} (#{error.class})"
  txt << "Backtrace:\n#{error.backtrace[0, trace_lines].join("\n\t")}"
end

#format_info_message(message) ⇒ Object



52
53
54
55
56
# File 'lib/worker_tools/recorder.rb', line 52

def format_info_message(message)
  return error_to_text(message, info_error_trace_lines) if message.is_a?(Exception)

  message
end

#format_log_message(message) ⇒ Object



46
47
48
49
50
# File 'lib/worker_tools/recorder.rb', line 46

def format_log_message(message)
  return error_to_text(message, log_error_trace_lines) if message.is_a?(Exception)

  message
end

#info_error_trace_linesObject



74
75
76
# File 'lib/worker_tools/recorder.rb', line 74

def info_error_trace_lines
  20
end

#log_directoryObject



62
63
64
# File 'lib/worker_tools/recorder.rb', line 62

def log_directory
  Rails.root.join('log')
end

#log_error_trace_linesObject



70
71
72
# File 'lib/worker_tools/recorder.rb', line 70

def log_error_trace_lines
  20
end

#log_file_nameObject



66
67
68
# File 'lib/worker_tools/recorder.rb', line 66

def log_file_name
  "#{self.class.name.underscore.tr('/', '_')}.log"
end

#loggerObject



58
59
60
# File 'lib/worker_tools/recorder.rb', line 58

def logger
  @logger ||= Logger.new(File.join(log_directory, log_file_name))
end

#record(message, level = :info) ⇒ Object



41
42
43
44
# File 'lib/worker_tools/recorder.rb', line 41

def record(message, level = :info)
  add_log(message, level)
  add_info(message)
end

#record_fail(error) ⇒ Object



25
26
27
28
29
30
# File 'lib/worker_tools/recorder.rb', line 25

def record_fail(error)
  record "ID #{model.id} - Error"
  record(error, :error)
  model.information = information
  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