Class: StandardCircuit::Notifiers::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/standard_circuit/notifiers/logger.rb

Overview

Subscribes to standard_circuit.circuit.* events and writes a human-readable log line for each transition. Always-on by default; pass a custom logger via ‘StandardCircuit.config.logger=`.

Constant Summary collapse

TRANSITION_EVENTS =
%w[
  standard_circuit.circuit.opened
  standard_circuit.circuit.closed
  standard_circuit.circuit.degraded
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(logger = nil) ⇒ Logger

Returns a new instance of Logger.



15
16
17
# File 'lib/standard_circuit/notifiers/logger.rb', line 15

def initialize(logger = nil)
  @logger = logger || ::Logger.new($stdout)
end

Instance Method Details

#call(event_name, payload) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/standard_circuit/notifiers/logger.rb', line 19

def call(event_name, payload)
  return unless TRANSITION_EVENTS.include?(event_name)

  message = build_message(event_name, payload)
  level = event_name == "standard_circuit.circuit.opened" ? :warn : :info
  @logger.public_send(level, message)
  message
end