ActiveRecord日志哪个查询转到哪个数据库

Way*_*rad 15 ruby logging rails-activerecord

在具有到不同数据库的多个连接的ActiveRecord应用程序中,日志中没有任何内容指示哪个查询进入哪个数据库.这些查询分离数据库:

Base1.connection.select_value("select * from foo")
Base2.connection.select_value("select * from foo")
Run Code Online (Sandbox Code Playgroud)

发出以下日志条目:

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
Run Code Online (Sandbox Code Playgroud)

如何使ActiveRecord数据库日志指示正在对哪个数据库执行查询?

独立的例子

begin
  require "bundler/inline"
rescue LoadError => 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 < ActiveRecord::Base
  self.abstract_class = true
end

class Base2 < 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
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)

我尝试为每个连接创建一个单独的记录器

我尝试为每个连接提供自己的记录器,以便我可以更改每个日志的格式:

Base1.logger = Logger.new(STDOUT)
Base2.logger = Logger.new(STDOUT)
Run Code Online (Sandbox Code Playgroud)

但是,不幸的是,ActiveRecord中似乎只有一个记录器,如此行所示,它不会引发异常:

raise unless Base1.logger.object_id == Base2.logger.object_id
Run Code Online (Sandbox Code Playgroud)

版本

  • 红宝石2.3.3
  • activerecord 4.2.8
  • 在这个例子中,sqlite3 1.13.3(1)
  • 在生产中,mysql2 0.4.5(1)和activerecord-sqlserver-adapter 4.2.15(1)

(1)此问题并非特定于任何特定数据库适配器.我已经列出了适配器版本的完整性.

ide*_*dej 5

据我所知,你不能用ActiveRecord本身做到这一点.但是你可以覆盖log方法,AbstractAdapter如果你真的需要它:

class ActiveRecord::ConnectionAdapters::AbstractAdapter
  alias :original_log :log
  def log(sql, name = "SQL", binds = [], statement_name = nil)
    #add info that you want to display to name
    name = "#{name} #{@connection.hash}" 
    original_log(sql, name, binds, statement_name) { yield }
  end
end
Run Code Online (Sandbox Code Playgroud)

输出:

D, [2017-03-15T20:55:59.200533 #73440] DEBUG -- :    -4111614587995646180 (0.5ms)  create table foo(i int)
D, [2017-03-15T20:55:59.201178 #73440] DEBUG -- :    -4097137311320758185 (0.1ms)  create table foo(i int)
D, [2017-03-15T20:55:59.201298 #73440] DEBUG -- :    -4111614587995646180 (0.0ms)  insert into foo(i) values (1)
D, [2017-03-15T20:55:59.201426 #73440] DEBUG -- :    -4097137311320758185 (0.1ms)  insert into foo(i) values (2)
D, [2017-03-15T20:55:59.202229 #73440] DEBUG -- :    -4111614587995646180 (0.7ms)  select * from foo
D, [2017-03-15T20:55:59.202360 #73440] DEBUG -- :    -4097137311320758185 (0.0ms)  select * from foo
Run Code Online (Sandbox Code Playgroud)

更加人性化

上面的代码记录了连接哈希值,这足以告诉另一个连接.如果你想记录比这更人性化的东西,你需要有点棘手.

我们可以做的是用一个方法来装饰抽象适配器,以返回一个人性化的连接名称.在程序初始化期间,将#log_name方法添加到每个连接适配器:

Base1.connection.define_singleton_method(:log_name) { "one" }
Base2.connection.define_singleton_method(:log_name) { "two" }
Run Code Online (Sandbox Code Playgroud)

猴子补丁现在可以使用#log_name方法:

class ActiveRecord::ConnectionAdapters::AbstractAdapter
  alias :original_log :log
  def log(sql, name = "SQL", binds = [], statement_name = nil)
    connection_name = respond_to?(:log_name) ? log_name : nil
    name = [connection_name, name].compact.join(" ")
    original_log(sql, name, binds, statement_name) { yield }
  end
end
Run Code Online (Sandbox Code Playgroud)

输出:

D, [2017-03-21T10:10:53.330021 #22147] DEBUG -- :   one (0.3ms)  create table foo(i int)
D, [2017-03-21T10:10:53.330380 #22147] DEBUG -- :   two (0.2ms)  create table foo(i int)
D, [2017-03-21T10:10:53.330464 #22147] DEBUG -- :   one (0.0ms)  insert into foo(i) values (1)
D, [2017-03-21T10:10:53.330536 #22147] DEBUG -- :   two (0.0ms)  insert into foo(i) values (2)
D, [2017-03-21T10:10:53.331002 #22147] DEBUG -- :   one (0.4ms)  select * from foo
D, [2017-03-21T10:10:53.331104 #22147] DEBUG -- :   two (0.0ms)  select * from foo
Run Code Online (Sandbox Code Playgroud)