aboutsummaryrefslogblamecommitdiffstats
path: root/actioncable/lib/action_cable/connection/tagged_logger_proxy.rb
blob: aef549aa86ce4a6b3e01e3b0e9b316e92dbf1847 (plain) (tree)
1
2
3
4
5
6
7
8

                   

                                                                                                                       
                                                                                                 
                           

                       




                                   




                             








                                                             





                                                                  

                                      
                                                     
           


       
module ActionCable
  module Connection
    # Allows the use of per-connection tags against the server logger. This wouldn't work using the traditional
    # <tt>ActiveSupport::TaggedLogging</tt> 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.
    class TaggedLoggerProxy
      attr_reader :tags

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

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

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

      %i( debug info warn error fatal unknown ).each do |severity|
        define_method(severity) do |message|
          log severity, message
        end
      end

      private
        def log(type, message) # :doc:
          tag(@logger) { @logger.send type, message }
        end
    end
  end
end