[英]How to let the SQL results display in Rails's log?
I don't know whether this is a reasonable request. 我不知道这是否合理。 I hope to see the result in the log file such as
log/development.log
, Now the ActiveRecord::Base.logger.level = 0
and there is only the SQL
statements: 我希望在日志文件中看到结果,例如
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
There is no results. 没有结果。 I know in the 'rails console', when using
ActiveRecord::Base.logger = Logger.new(STDOUT)
, I see the statements and corresponding results: 我知道在“ rails控制台”中,当使用
ActiveRecord::Base.logger = Logger.new(STDOUT)
,我看到了以下语句和相应的结果:
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>
What options I can set for this? 我可以为此设置哪些选项? Thanks in advance!
提前致谢!
I researched some ActiveRecord (v3.2.21) code, It seems that there is no option about the SQL result. 我研究了一些ActiveRecord(v3.2.21)代码,似乎没有关于SQL结果的选项。 The above log
User Load (1.0ms) SELECT "users".* FROM "users" LIMIT 1
is finished through calling ActiveSupport::Notifications.instrument()
: 上面的日志
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
So if I want to see the result in log I can add a log statement: 因此,如果我想在日志中查看结果,可以添加一条日志语句:
logger.info "#{result}"
result.collect { |row| row.dup }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.