mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Move AR logging responsibilities to ActiveRecord::Railties::Subscriber.
This commit is contained in:
parent
fe3ceabeed
commit
dc2e291a93
5 changed files with 35 additions and 46 deletions
|
@ -75,7 +75,8 @@ module ActiveRecord
|
|||
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 @@ module ActiveRecord
|
|||
"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 @@ module ActiveRecord
|
|||
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 @@ module ActiveRecord
|
|||
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 @@ module ActiveRecord
|
|||
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 @@ module ActiveRecord
|
|||
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
|
|
@ -138,14 +138,3 @@ class PooledConnectionsTest < ActiveRecord::TestCase
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue