ActiveRecord log which query went to which database : <p>In an ActiveRecord application with multiple connections to different databases, nothing in the log indicates which query went to which database.  These queries to separate databases:</p>

<pre><code>Base1.connection.select_value("select * from foo")
Base2.connection.select_value("select * from foo")
</code></pre>

<p>Emit these log entries:</p>

<pre><code>D, [2017-03-13T09:27:11.844395 #22112] DEBUG -- :    (0.6ms)  select * from foo
D, [2017-03-13T09:27:11.844539 #22112] DEBUG -- :    (0.1ms)  select * from foo
</code></pre>

<p>How can I cause the ActiveRecord database log to indicate which database a query was being done against?</p>

<h1>Standalone example</h1>

<pre><code>begin
  require "bundler/inline"
rescue LoadError =&gt; e
  $stderr.puts "Bundler version 1.10 or later is required."
  raise e
end

gemfile(true) do
  source "https://rubygems.org"
  # Activate the gem you are reporting the issue against.
  gem "activerecord", "4.2.8"
  gem "sqlite3"
end

require "active_record"
require "logger"

class Base1 &lt; ActiveRecord::Base
  self.abstract_class = true
end

class Base2 &lt; ActiveRecord::Base
  self.abstract_class = true
end

Base1.establish_connection(adapter: "sqlite3", database: ":memory:")
Base2.establish_connection(adapter: "sqlite3", database: ":memory:")

ActiveRecord::Base.logger = Logger.new(STDOUT)

Base1.connection.execute("create table foo(i int)")
Base2.connection.execute("create table foo(i int)")
Base1.connection.execute("insert into foo(i) values (1)")
Base2.connection.execute("insert into foo(i) values (2)")
raise unless Base1.connection.select_value("select * from foo") == 1
raise unless Base2.connection.select_value("select * from foo") == 2
</code></pre>

<p>The output:</p>

<pre><code>D, [2017-03-13T09:27:11.842939 #22112] DEBUG -- :    (0.2ms)  create table foo(i int)
D, [2017-03-13T09:27:11.843478 #22112] DEBUG -- :    (0.2ms)  create table foo(i int)
D, [2017-03-13T09:27:11.843612 #22112] DEBUG -- :    (0.1ms)  insert into foo(i) values (1)
D, [2017-03-13T09:27:11.843720 #22112] DEBUG -- :    (0.0ms)  insert into foo(i) values (2)
D, [2017-03-13T09:27:11.844395 #22112] DEBUG -- :    (0.6ms)  select * from foo
D, [2017-03-13T09:27:11.844539 #22112] DEBUG -- :    (0.1ms)  select * from foo
</code></pre>

<h1>I tried creating a separate logger for each connection</h1>

<p>I tried giving each connection its own logger, so that I could change each log's formatting:</p>

<pre><code>Base1.logger = Logger.new(STDOUT)
Base2.logger = Logger.new(STDOUT)
</code></pre>

<p>But, unfortunately, there appears to be only one logger in ActiveRecord, as shown by this line which does not raise an exception:</p>

<pre><code>raise unless Base1.logger.object_id == Base2.logger.object_id
</code></pre>

<h1>Versions</h1>

<ul>
<li>ruby-2.3.3</li>
<li>activerecord 4.2.8</li>
<li>In this example, sqlite3 1.13.3 (1)</li>
<li>In production, mysql2 0.4.5 (1) and activerecord-sqlserver-adapter 4.2.15 (1)</li>
</ul>

<p>(1) This problem is not specific to any particular database adapter.  I've listed adapter versions for completeness.</p>
