Class: ActionCable::Server::TaggedLoggerProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/action_cable/server/tagged_logger_proxy.rb

Overview

# Action Cable Connection TaggedLoggerProxy

Allows the use of per-connection tags against the server logger. This wouldn’t work using the traditional ActiveSupport::TaggedLogging enhanced Rails.logger, as that logger will reset the tags between requests. The connection is long-lived, so it needs its own set of tags for its independent duration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, tags:) ⇒ TaggedLoggerProxy

Returns a new instance of TaggedLoggerProxy.



15
16
17
18
# File 'lib/action_cable/server/tagged_logger_proxy.rb', line 15

def initialize(logger, tags:)
  @logger = logger
  @tags = tags.flatten
end

Instance Attribute Details

#tagsObject (readonly)

Returns the value of attribute tags.



13
14
15
# File 'lib/action_cable/server/tagged_logger_proxy.rb', line 13

def tags
  @tags
end

Instance Method Details

#add_tags(*tags) ⇒ Object



20
21
22
23
# File 'lib/action_cable/server/tagged_logger_proxy.rb', line 20

def add_tags(*tags)
  @tags += tags.flatten
  @tags = @tags.uniq
end

#tag(logger, &block) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/action_cable/server/tagged_logger_proxy.rb', line 25

def tag(logger, &block)
  if logger.respond_to?(:tagged)
    current_tags = tags - logger.formatter.current_tags
    logger.tagged(*current_tags, &block)
  else
    yield
  end
end