diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2015-12-11 17:22:12 +0100 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2015-12-11 17:22:12 +0100 |
commit | cf4d9aa992f99ca19fd32446d081ba7d145fbeae (patch) | |
tree | f16d0a3985dc40ae8b5013ebeca1e7e188b1b530 /lib | |
parent | 2b4aa9a173eeda96a8d827c5bdd9ad8e41e6abf1 (diff) | |
parent | 43c6c7787954d959347a1f90d71b11ba0cb7a8c7 (diff) | |
download | rails-cf4d9aa992f99ca19fd32446d081ba7d145fbeae.tar.gz rails-cf4d9aa992f99ca19fd32446d081ba7d145fbeae.tar.bz2 rails-cf4d9aa992f99ca19fd32446d081ba7d145fbeae.zip |
Merge pull request #125 from lsylvester/handle-custom-loggers
Handle cases where logger is not a tagged logger.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/action_cable/connection/base.rb | 8 | ||||
-rw-r--r-- | lib/action_cable/connection/tagged_logger_proxy.rb | 12 | ||||
-rw-r--r-- | lib/action_cable/server/worker/active_record_connection_management.rb | 2 |
3 files changed, 14 insertions, 8 deletions
diff --git a/lib/action_cable/connection/base.rb b/lib/action_cable/connection/base.rb index 6df168e4c3..b93b6a8a50 100644 --- a/lib/action_cable/connection/base.rb +++ b/lib/action_cable/connection/base.rb @@ -56,7 +56,7 @@ module ActionCable def initialize(server, env) @server, @env = server, env - @logger = new_tagged_logger || server.logger + @logger = new_tagged_logger @websocket = ActionCable::Connection::WebSocket.new(env) @subscriptions = ActionCable::Connection::Subscriptions.new(self) @@ -194,10 +194,8 @@ module ActionCable # Tags are declared in the server but computed in the connection. This allows us per-connection tailored tags. def new_tagged_logger - if server.logger.respond_to?(:tagged) - TaggedLoggerProxy.new server.logger, - tags: server.config.log_tags.map { |tag| tag.respond_to?(:call) ? tag.call(request) : tag.to_s.camelize } - end + TaggedLoggerProxy.new server.logger, + tags: server.config.log_tags.map { |tag| tag.respond_to?(:call) ? tag.call(request) : tag.to_s.camelize } end def started_request_message diff --git a/lib/action_cable/connection/tagged_logger_proxy.rb b/lib/action_cable/connection/tagged_logger_proxy.rb index 34063c1d42..e5319087fb 100644 --- a/lib/action_cable/connection/tagged_logger_proxy.rb +++ b/lib/action_cable/connection/tagged_logger_proxy.rb @@ -16,6 +16,15 @@ module ActionCable @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 @@ -24,8 +33,7 @@ module ActionCable protected def log(type, message) - current_tags = tags - @logger.formatter.current_tags - @logger.tagged(*current_tags) { @logger.send type, message } + tag(@logger) { @logger.send type, message } end end end diff --git a/lib/action_cable/server/worker/active_record_connection_management.rb b/lib/action_cable/server/worker/active_record_connection_management.rb index 1ede0095f8..ecece4e270 100644 --- a/lib/action_cable/server/worker/active_record_connection_management.rb +++ b/lib/action_cable/server/worker/active_record_connection_management.rb @@ -12,7 +12,7 @@ module ActionCable end def with_database_connections - ActiveRecord::Base.logger.tagged(*connection.logger.tags) { yield } + connection.logger.tag(ActiveRecord::Base.logger) { yield } ensure ActiveRecord::Base.clear_active_connections! end |