Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging of connection? #13

Open
psharpNumerex opened this issue Mar 28, 2013 · 2 comments
Open

Logging of connection? #13

psharpNumerex opened this issue Mar 28, 2013 · 2 comments

Comments

@psharpNumerex
Copy link

Is there a way to tell which connection is being used by looking at the logs?

I can see where logger is passed into the SDPAdaptor, but it seems like the log messages are all for errors.

@bdurand
Copy link
Owner

bdurand commented Jun 11, 2013

I can't see any good way of adding logging that would work across database adapters and that wouldn't be too chatty.

You could mix in something to you specific connections to output log entries when they are being used.

I you can come up with a good general way of solving this please submit a pull request.

@printercu
Copy link

I have pick some code from db_charmer and modified it for sdbp. But get_connection_name should be fixed. I couldn't find easy way to get proper connection name for now.

You can check out originals here:

require 'active_record/log_subscriber'

module SeamlessDatabasePool
  module ActiveRecord
    module ConnectionAdapters
      class AbstractAdapter
        module ConnectionName
          # We use this proxy to push connection name down to instrumenters
          # w/o monkey-patching the log method itself
          class InstrumenterDecorator < ActiveSupport::BasicObject
            def initialize(adapter, instrumenter)
              @adapter = adapter
              @instrumenter = instrumenter
            end

            def instrument(name, payload = {}, &block)
              payload[:connection_name] ||= get_connection_name
              @instrumenter.instrument(name, payload, &block)
            end

            def method_missing(meth, *args, &block)
              @instrumenter.send(meth, *args, &block)
            end

            private
              def get_connection_name
                ::SeamlessDatabasePool.read_only_connection_type
              end
          end

          def self.included(base)
            base.alias_method_chain :initialize, :connection_name
          end

          def connection_name
            raise "Can't find connection configuration!" unless @config
            @config[:connection_name]
          end

          def initialize_with_connection_name(*args)
            initialize_without_connection_name(*args)
            @instrumenter = InstrumenterDecorator.new(self, @instrumenter)
          end
        end

        ::ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, ConnectionName)
      end
    end

    module LogSubscriber
      def self.included(base)
        base.send(:attr_accessor, :connection_name)
        base.alias_method_chain :sql, :connection_name
        base.alias_method_chain :debug, :connection_name
      end

      def sql_with_connection_name(event)
        self.connection_name = event.payload[:connection_name]
        sql_without_connection_name(event)
      end

      def debug_with_connection_name(msg)
        conn = connection_name ? color("  [#{connection_name}]", ::ActiveSupport::LogSubscriber::BLUE, true) : ''
        debug_without_connection_name(conn + msg)
      end
    end

    ::ActiveRecord::LogSubscriber.send(:include, LogSubscriber)
  end
end

You can test this code adding it right after require 'rails/all' in application.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants