Module: CapistranoSentinel::Logging

Included in:
ApplicationHelper
Defined in:
lib/capistrano_sentinel/helpers/logging.rb

Overview

class that holds the options that are configurable for this gem

Class Method Summary collapse

Class Method Details

.error_filtered?(error) ⇒ Boolean


14
15
16
# File 'lib/capistrano_sentinel/helpers/logging.rb', line 14

def error_filtered?(error)
  [SystemExit].find { |class_name| error.is_a?(class_name) }.present?
end

.execute_with_rescue(output = nil) ⇒ Object


67
68
69
70
71
72
73
# File 'lib/capistrano_sentinel/helpers/logging.rb', line 67

def execute_with_rescue(output = nil)
  yield if block_given?
rescue Interrupt
  rescue_interrupt
rescue => error
  rescue_error(error, output)
end

.find_worker_log(job_id) ⇒ Object


46
47
48
49
50
51
# File 'lib/capistrano_sentinel/helpers/logging.rb', line 46

def find_worker_log(job_id)
  return if job_id.blank?
  FileUtils.mkdir_p(log_directory) unless File.directory?(log_directory)
  filename = File.join(log_directory, "worker_#{job_id}.log")
  setup_filename_logger(filename)
end

.format_error(exception) ⇒ Object


29
30
31
32
33
34
# File 'lib/capistrano_sentinel/helpers/logging.rb', line 29

def format_error(exception)
  message = "\n#{exception.class} (#{exception.respond_to?(:message) ? exception.message : exception.inspect}):\n"
  message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code)
  message << '  ' << exception.backtrace.join("\n  ") if exception.respond_to?(:backtrace)
  message
end

.log_error(error, options = {}) ⇒ Object


18
19
20
21
22
# File 'lib/capistrano_sentinel/helpers/logging.rb', line 18

def log_error(error, options = {})
  message = format_error(error)
  log_output_error(error, options.fetch(:output, nil), message)
  log_to_file(message, options.merge(log_method: 'fatal'))
end

.log_output_error(error, output, message) ⇒ Object


24
25
26
27
# File 'lib/capistrano_sentinel/helpers/logging.rb', line 24

def log_output_error(error, output, message)
  return if message.blank? || error_filtered?(error)
  puts message if output.present?
end

.log_to_file(message, options = {}) ⇒ Object


36
37
38
39
40
# File 'lib/capistrano_sentinel/helpers/logging.rb', line 36

def log_to_file(message, options = {})
  return unless logging_enabled?
  worker_log = options.fetch(:job_id, '').present? ? find_worker_log(options[:job_id]) : logger
  print_to_log_file(worker_log, options.merge(message: message)) if worker_log.present?
end

.loggerObject


10
11
12
# File 'lib/capistrano_sentinel/helpers/logging.rb', line 10

def logger
  @logger ||= ::Logger.new(ENV["LOG_FILE"] || '/dev/null')
end

.logging_enabled?Boolean


6
7
8
# File 'lib/capistrano_sentinel/helpers/logging.rb', line 6

def logging_enabled?
  ENV["WEBSOCKET_LOGGING"].to_s == 'true'
end

42
43
44
# File 'lib/capistrano_sentinel/helpers/logging.rb', line 42

def print_to_log_file(worker_log, options = {})
  worker_log.send(options.fetch(:log_method, 'debug'), "#{options.fetch(:message, '')}\n")
end

.rescue_error(error, output = nil) ⇒ Object


75
76
77
78
# File 'lib/capistrano_sentinel/helpers/logging.rb', line 75

def rescue_error(error, output = nil)
  log_error(error, output: output)
  exit(1)
end

.setup_filename_logger(filename) ⇒ Object


53
54
55
56
57
58
# File 'lib/capistrano_sentinel/helpers/logging.rb', line 53

def setup_filename_logger(filename)
  worker_log = ::Logger.new(filename)
  worker_log.level = ::Logger::Severity::DEBUG
  setup_logger_formatter(worker_log)
  worker_log
end

.setup_logger_formatter(logger) ⇒ Object


60
61
62
63
64
65
# File 'lib/capistrano_sentinel/helpers/logging.rb', line 60

def setup_logger_formatter(logger)
  logger.formatter = proc do |severity, datetime, progname, msg|
    date_format = datetime.strftime('%Y-%m-%d %H:%M:%S')
    "[#{date_format}] #{severity}  (#{progname}): #{msg}\n"
  end
end

.show_warning(message) ⇒ Object


80
81
82
# File 'lib/capistrano_sentinel/helpers/logging.rb', line 80

def show_warning(message)
  warn message
end