Class: CommandTower::Logging::Subscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/command_tower/logging/subscriber.rb

Constant Summary collapse

PATTERNS =
[
  /\Acommand_tower\.lifecycle(?:\.|\z)/,
  /\Acommand_tower\.log(?:\.|\z)/,
  /\Acommand_tower\.messaging(?:\.|\z)/
].freeze
SEVERITIES =
%i[debug info warn error].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attach!Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/command_tower/logging/subscriber.rb', line 22

def attach!
  detach!
  subscriber = new
  @subscriptions = PATTERNS.map do |pattern|
    ActiveSupport::Notifications.subscribe(pattern) do |name, started, finished, id, payload|
      event = ActiveSupport::Notifications::Event.new(name, started, finished, id, payload)
      subscriber.call(event)
    end
  end
end

.detach!Object



33
34
35
36
# File 'lib/command_tower/logging/subscriber.rb', line 33

def detach!
  Array(@subscriptions).each { |subscription| ActiveSupport::Notifications.unsubscribe(subscription) }
  @subscriptions = []
end

.loggerObject



14
15
16
# File 'lib/command_tower/logging/subscriber.rb', line 14

def logger
  @logger || super
end

.logger=(value) ⇒ Object



18
19
20
# File 'lib/command_tower/logging/subscriber.rb', line 18

def logger=(value)
  @logger = value
end

.subscriptionsObject



38
39
40
# File 'lib/command_tower/logging/subscriber.rb', line 38

def subscriptions
  Array(@subscriptions)
end

Instance Method Details

#call(event) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/command_tower/logging/subscriber.rb', line 43

def call(event)
  return if logger.nil?
  return unless materialize?(event)

  severity = severity_for(event)
  logger.public_send(severity, log_entry(event))
rescue StandardError
  begin
    logger&.error({ event: "command_tower.logging.subscriber_failed", error_class: $!.class.name })
  rescue StandardError
    nil
  end
end

#loggerObject



57
58
59
# File 'lib/command_tower/logging/subscriber.rb', line 57

def logger
  self.class.logger
end