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
- .error_filtered?(error) ⇒ Boolean
- .execute_with_rescue(output = nil) ⇒ Object
- .find_worker_log(job_id) ⇒ Object
- .format_error(exception) ⇒ Object
- .log_error(error, options = {}) ⇒ Object
- .log_output_error(error, output, message) ⇒ Object
- .log_to_file(message, options = {}) ⇒ Object
- .logger ⇒ Object
- .logging_enabled? ⇒ Boolean
- .print_to_log_file(worker_log, options = {}) ⇒ Object
- .rescue_error(error, output = nil) ⇒ Object
- .setup_filename_logger(filename) ⇒ Object
- .setup_logger_formatter(logger) ⇒ Object
- .show_warning(message) ⇒ Object
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) = "\n#{exception.class} (#{exception.respond_to?(:message) ? exception. : exception.inspect}):\n" << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code) << ' ' << exception.backtrace.join("\n ") if exception.respond_to?(:backtrace) end |
.log_error(error, options = {}) ⇒ Object
18 19 20 21 22 |
# File 'lib/capistrano_sentinel/helpers/logging.rb', line 18 def log_error(error, = {}) = format_error(error) log_output_error(error, .fetch(:output, nil), ) log_to_file(, .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, ) return if .blank? || error_filtered?(error) puts 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(, = {}) return unless logging_enabled? worker_log = .fetch(:job_id, '').present? ? find_worker_log([:job_id]) : logger print_to_log_file(worker_log, .merge(message: )) if worker_log.present? end |
.logger ⇒ Object
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 |
.print_to_log_file(worker_log, options = {}) ⇒ Object
42 43 44 |
# File 'lib/capistrano_sentinel/helpers/logging.rb', line 42 def print_to_log_file(worker_log, = {}) worker_log.send(.fetch(:log_method, 'debug'), "#{.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() warn end |