Class: ChiliLogger::LoggingErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/errors/logging_error_handler/logging_error_handler.rb

Overview

class that handles errors when message broker can’t be reached, etc…

Instance Method Summary collapse

Constructor Details

#initialize(fallback_name = nil, config = {}) ⇒ LoggingErrorHandler

Returns a new instance of LoggingErrorHandler.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/errors/logging_error_handler/logging_error_handler.rb', line 8

def initialize(fallback_name = nil, config = {})
  @default = ChiliLogger::Values::Default.new
  config ||= {}
  validate_config(config)

  fallback_name ||= @default.fallback_broker
  fallback_broker_class = supported_fallback_brokers[fallback_name.to_sym]
  unsupported_fallback_broker_error unless fallback_broker_class

  @fallback_broker = fallback_broker_class.new(config)
end

Instance Method Details

#delete_unpublished_log(msg) ⇒ Object



37
38
39
# File 'lib/errors/logging_error_handler/logging_error_handler.rb', line 37

def delete_unpublished_log(msg)
  @fallback_broker.delete_message(msg)
end

#fetch_unpublished_logsObject



33
34
35
# File 'lib/errors/logging_error_handler/logging_error_handler.rb', line 33

def fetch_unpublished_logs
  @fallback_broker.fetch_messages
end

#handle_error(error, log = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/errors/logging_error_handler/logging_error_handler.rb', line 20

def handle_error(error, log = nil)
  return if ChiliLogger.instance.deactivated

  message = message(error, log)
  @fallback_broker.publish(message)
rescue StandardError => e
  puts 'There was a problem with both the Message Broker and the Fallback Broker simultaneously.
    To keep the application running and prevent downtime,
    ChiliLogger will ignore this errors and discard the log it was currently trying to publish.
    Please note that logs are being permanently lost.'
  puts e
end