我不知道这是否是合理的要求。我希望在日志文件中看到结果,例如log/development.log
,现在是ActiveRecord::Base.logger.level = 0
,而且只有SQL
语句:
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1
Excursion Load (0.3ms) SELECT "excursions".* FROM "excursions" WHERE "excursions"."id" = $1 LIMIT 1 [["id", "5"]]
Actor Load (0.5ms) SELECT "actors".* FROM "actors" WHERE "actors"."id" = 4 LIMIT 1
没有结果。我知道在使用ActiveRecord::Base.logger = Logger.new(STDOUT)
的' rails控制台中,我看到了这些陈述和相应的结果:
1.9.3-p551 :001 > ActiveRecord::Base.logger = Logger.new(STDOUT)
=> #<Logger:0x007f80441364d0 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007f8044135c60 @datetime_format=nil>, @formatter=# <Logger::SimpleFormatter:0x007f8044134bd0 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x007f8044135440 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mutex=# <Logger::LogDevice::LogDeviceMutex:0x007f8044135378 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007f8044134c98>>>>
1.9.3-p551 :002 > User.first
User Load (1.0ms) SELECT "users".* FROM "users" LIMIT 1
Actor Load (0.8ms) SELECT "actors".* FROM "actors" WHERE "actors"."id" = 2 LIMIT 1
ActivityObject Load (0.9ms) SELECT "activity_objects".* FROM "activity_objects" WHERE "activity_objects"."id" = 2 LIMIT 1
=> #<User id: 1, encrypted_password: "$2a$10$wFj.Q9XX0ua16BF0.SIps.VjnfVi8/6egicirc/SFxw3...", password_salt: nil, reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, authentication_token: nil, created_at: "2015-03-25 22:14:32", updated_at: "2015-03-25 22:14:32", actor_id: 2, language: nil, connected: false, status: "chat", chat_enabled: true, occupation: nil, invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_id: nil, invited_by_type: nil>
我可以为此设置哪些选项?提前致谢!
答案 0 :(得分:0)
我研究了一些ActiveRecord(v3.2.21)代码,似乎没有关于SQL结果的选项。上述日志User Load (1.0ms) SELECT "users".* FROM "users" LIMIT 1
已通过调用ActiveSupport::Notifications.instrument()
完成:
# query_cache.rb
def cache_sql(sql, binds)
result =
if @query_cache[sql].key?(binds)
ActiveSupport::Notifications.instrument("sql.active_record",
:sql => sql, :binds => binds, :name => "CACHE", :connection_id => object_id)
@query_cache[sql][binds]
else
@query_cache[sql][binds] = yield
end
result.collect { |row| row.dup }
end
因此,如果我想在日志中看到结果,我可以添加一个日志语句:
logger.info "#{result}"
result.collect { |row| row.dup }