class Faulty::Events::LogListener

A default listener that logs Faulty events

Attributes

logger[R]

Public Class Methods

new(logger = nil) click to toggle source

@param logger A logger similar to stdlib `Logger`. Uses the Rails logger

by default if available, otherwise it creates a new `Logger` to
stderr.
# File lib/faulty/events/log_listener.rb, line 12
def initialize(logger = nil)
  logger ||= defined?(Rails) ? Rails.logger : ::Logger.new($stderr)
  @logger = logger
end

Public Instance Methods

handle(event, payload) click to toggle source

(see ListenerInterface#handle)

# File lib/faulty/events/log_listener.rb, line 18
def handle(event, payload)
  return unless EVENT_SET.include?(event)

  send(event, payload)
end

Private Instance Methods

cache_failure(payload) click to toggle source
# File lib/faulty/events/log_listener.rb, line 66
def cache_failure(payload)
  log(
    :error, 'Cache failure', payload[:action],
    key: payload[:key],
    error: payload[:error].message
  )
end
circuit_cache_hit(payload) click to toggle source
# File lib/faulty/events/log_listener.rb, line 26
def circuit_cache_hit(payload)
  log(:debug, 'Circuit cache hit', payload[:circuit].name, key: payload[:key])
end
circuit_cache_miss(payload) click to toggle source
# File lib/faulty/events/log_listener.rb, line 30
def circuit_cache_miss(payload)
  log(:debug, 'Circuit cache miss', payload[:circuit].name, key: payload[:key])
end
circuit_cache_write(payload) click to toggle source
# File lib/faulty/events/log_listener.rb, line 34
def circuit_cache_write(payload)
  log(:debug, 'Circuit cache write', payload[:circuit].name, key: payload[:key])
end
circuit_closed(payload) click to toggle source
# File lib/faulty/events/log_listener.rb, line 62
def circuit_closed(payload)
  log(:info, 'Circuit closed', payload[:circuit].name)
end
circuit_failure(payload) click to toggle source
# File lib/faulty/events/log_listener.rb, line 42
def circuit_failure(payload)
  log(
    :error, 'Circuit failed', payload[:circuit].name,
    state: payload[:status].state,
    error: payload[:error].message
  )
end
circuit_opened(payload) click to toggle source
# File lib/faulty/events/log_listener.rb, line 54
def circuit_opened(payload)
  log(:error, 'Circuit opened', payload[:circuit].name, error: payload[:error].message)
end
circuit_reopened(payload) click to toggle source
# File lib/faulty/events/log_listener.rb, line 58
def circuit_reopened(payload)
  log(:error, 'Circuit reopened', payload[:circuit].name, error: payload[:error].message)
end
circuit_skipped(payload) click to toggle source
# File lib/faulty/events/log_listener.rb, line 50
def circuit_skipped(payload)
  log(:warn, 'Circuit skipped', payload[:circuit].name)
end
circuit_success(payload) click to toggle source
# File lib/faulty/events/log_listener.rb, line 38
def circuit_success(payload)
  log(:debug, 'Circuit succeeded', payload[:circuit].name)
end
log(level, msg, action, extra = {}) click to toggle source
# File lib/faulty/events/log_listener.rb, line 81
def log(level, msg, action, extra = {})
  @logger.public_send(level) do
    extra_str = extra.map { |k, v| "#{k}=#{v}" }.join(' ')
    extra_str = " #{extra_str}" unless extra_str.empty?

    "#{msg}: #{action}#{extra_str}"
  end
end
storage_failure(payload) click to toggle source
# File lib/faulty/events/log_listener.rb, line 74
def storage_failure(payload)
  extra = {}
  extra[:circuit] = payload[:circuit].name if payload.key?(:circuit)
  extra[:error] = payload[:error].message
  log(:error, 'Storage failure', payload[:action], extra)
end