Move AR logging responsibilities to ActiveRecord::Railties::Subscriber.
This commit is contained in:
parent
fe3ceabeed
commit
dc2e291a93
@ -75,7 +75,8 @@ def columns_with_query_cache(*args)
|
||||
def cache_sql(sql)
|
||||
result =
|
||||
if @query_cache.has_key?(sql)
|
||||
log_info(sql, "CACHE", 0.0)
|
||||
ActiveSupport::Notifications.instrument("active_record.sql",
|
||||
:sql => sql, :name => "CACHE", :connection_id => self.object_id)
|
||||
@query_cache[sql]
|
||||
else
|
||||
@query_cache[sql] = yield
|
||||
|
@ -190,13 +190,6 @@ def current_savepoint_name
|
||||
"active_record_#{open_transactions}"
|
||||
end
|
||||
|
||||
def log_info(sql, name, ms)
|
||||
if @logger && @logger.debug?
|
||||
name = '%s (%.1fms)' % [name, ms]
|
||||
@logger.debug(format_log_entry(name, sql.squeeze(' ')))
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
def log(sql, name)
|
||||
name ||= "SQL"
|
||||
@ -208,7 +201,7 @@ def log(sql, name)
|
||||
result
|
||||
rescue Exception => e
|
||||
message = "#{e.class.name}: #{e.message}: #{sql}"
|
||||
log_info(message, name, 0)
|
||||
@logger.debug message if @logger
|
||||
raise translate_exception(e, message)
|
||||
end
|
||||
|
||||
@ -217,23 +210,6 @@ def translate_exception(e, message)
|
||||
ActiveRecord::StatementInvalid.new(message)
|
||||
end
|
||||
|
||||
def format_log_entry(message, dump = nil)
|
||||
if ActiveRecord::Base.colorize_logging
|
||||
if @@row_even
|
||||
@@row_even = false
|
||||
message_color, dump_color = "4;36;1", "0;1"
|
||||
else
|
||||
@@row_even = true
|
||||
message_color, dump_color = "4;35;1", "0"
|
||||
end
|
||||
|
||||
log_entry = " \e[#{message_color}m#{message}\e[0m "
|
||||
log_entry << "\e[#{dump_color}m%#{String === dump ? 's' : 'p'}\e[0m" % dump if dump
|
||||
log_entry
|
||||
else
|
||||
"%s %s" % [message, dump]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -14,6 +14,10 @@ class Railtie < Rails::Railtie
|
||||
load "active_record/railties/databases.rake"
|
||||
end
|
||||
|
||||
# TODO If we require the wrong file, the error never comes up.
|
||||
require "active_record/railties/subscriber"
|
||||
subscriber ActiveRecord::Railties::Subscriber.new
|
||||
|
||||
initializer "active_record.set_configs" do |app|
|
||||
app.config.active_record.each do |k,v|
|
||||
ActiveRecord::Base.send "#{k}=", v
|
||||
@ -59,14 +63,6 @@ class Railtie < Rails::Railtie
|
||||
ActiveRecord::Base.logger ||= ::Rails.logger
|
||||
end
|
||||
|
||||
initializer "active_record.notifications" do
|
||||
require 'active_support/notifications'
|
||||
|
||||
ActiveSupport::Notifications.subscribe("active_record.sql") do |name, before, after, instrumenter_id, payload|
|
||||
payload[:connection].log_info(payload[:sql], payload[:name], (after - before) * 1000)
|
||||
end
|
||||
end
|
||||
|
||||
initializer "active_record.i18n_deprecation" do
|
||||
require 'active_support/i18n'
|
||||
|
||||
|
27
activerecord/lib/active_record/railties/subscriber.rb
Normal file
27
activerecord/lib/active_record/railties/subscriber.rb
Normal file
@ -0,0 +1,27 @@
|
||||
module ActiveRecord
|
||||
module Railties
|
||||
class Subscriber < Rails::Subscriber
|
||||
def sql(event)
|
||||
name = '%s (%.1fms)' % [event.payload[:name], event.duration]
|
||||
sql = event.payload[:sql].squeeze(' ')
|
||||
|
||||
if odd?
|
||||
name = color(name, :cyan, true)
|
||||
sql = color(sql, nil, true)
|
||||
else
|
||||
name = color(name, :magenta, true)
|
||||
end
|
||||
|
||||
debug " #{name} #{sql}"
|
||||
end
|
||||
|
||||
def odd?
|
||||
@odd_or_even = !@odd_or_even
|
||||
end
|
||||
|
||||
def logger
|
||||
ActiveRecord::Base.logger
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -137,15 +137,4 @@ def test_connection_pool_callbacks
|
||||
def add_record(name)
|
||||
ActiveRecord::Base.connection_pool.with_connection { Project.create! :name => name }
|
||||
end
|
||||
end unless %w(FrontBase).include? ActiveRecord::Base.connection.adapter_name
|
||||
|
||||
class AllowConcurrencyDeprecatedTest < ActiveRecord::TestCase
|
||||
def test_allow_concurrency_is_deprecated
|
||||
assert_deprecated('ActiveRecord::Base.allow_concurrency') do
|
||||
ActiveRecord::Base.allow_concurrency
|
||||
end
|
||||
assert_deprecated('ActiveRecord::Base.allow_concurrency=') do
|
||||
ActiveRecord::Base.allow_concurrency = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end unless %w(FrontBase).include? ActiveRecord::Base.connection.adapter_name
|
Loading…
Reference in New Issue
Block a user