From bf40bddfceebaff637161be6c5d992d6978679ff Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 14 Dec 2015 15:48:54 +0100 Subject: Get ready to merge into Rails --- .../action_cable/connection/tagged_logger_proxy.rb | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 actioncable/lib/action_cable/connection/tagged_logger_proxy.rb (limited to 'actioncable/lib/action_cable/connection/tagged_logger_proxy.rb') diff --git a/actioncable/lib/action_cable/connection/tagged_logger_proxy.rb b/actioncable/lib/action_cable/connection/tagged_logger_proxy.rb new file mode 100644 index 0000000000..e5319087fb --- /dev/null +++ b/actioncable/lib/action_cable/connection/tagged_logger_proxy.rb @@ -0,0 +1,40 @@ +module ActionCable + module Connection + # Allows the use of per-connection tags against the server logger. This wouldn't work using the tradional + # 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. + 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 + + protected + def log(type, message) + tag(@logger) { @logger.send type, message } + end + end + end +end -- cgit v1.2.3