1
0
Fork 0
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:
José Valim 2010-01-12 18:29:48 +01:00
parent fe3ceabeed
commit dc2e291a93
5 changed files with 35 additions and 46 deletions

View file

@ -75,7 +75,8 @@ module ActiveRecord
def cache_sql(sql) def cache_sql(sql)
result = result =
if @query_cache.has_key?(sql) 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] @query_cache[sql]
else else
@query_cache[sql] = yield @query_cache[sql] = yield

View file

@ -190,13 +190,6 @@ module ActiveRecord
"active_record_#{open_transactions}" "active_record_#{open_transactions}"
end 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 protected
def log(sql, name) def log(sql, name)
name ||= "SQL" name ||= "SQL"
@ -208,7 +201,7 @@ module ActiveRecord
result result
rescue Exception => e rescue Exception => e
message = "#{e.class.name}: #{e.message}: #{sql}" message = "#{e.class.name}: #{e.message}: #{sql}"
log_info(message, name, 0) @logger.debug message if @logger
raise translate_exception(e, message) raise translate_exception(e, message)
end end
@ -217,23 +210,6 @@ module ActiveRecord
ActiveRecord::StatementInvalid.new(message) ActiveRecord::StatementInvalid.new(message)
end 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 end
end end

View file

@ -14,6 +14,10 @@ module ActiveRecord
load "active_record/railties/databases.rake" load "active_record/railties/databases.rake"
end 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| initializer "active_record.set_configs" do |app|
app.config.active_record.each do |k,v| app.config.active_record.each do |k,v|
ActiveRecord::Base.send "#{k}=", v ActiveRecord::Base.send "#{k}=", v
@ -59,14 +63,6 @@ module ActiveRecord
ActiveRecord::Base.logger ||= ::Rails.logger ActiveRecord::Base.logger ||= ::Rails.logger
end 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 initializer "active_record.i18n_deprecation" do
require 'active_support/i18n' require 'active_support/i18n'

View 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

View file

@ -138,14 +138,3 @@ class PooledConnectionsTest < ActiveRecord::TestCase
ActiveRecord::Base.connection_pool.with_connection { Project.create! :name => name } ActiveRecord::Base.connection_pool.with_connection { Project.create! :name => name }
end end
end unless %w(FrontBase).include? ActiveRecord::Base.connection.adapter_name 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