使用 Rails 4 在日志中显示 SQL 查询 [英] Display SQL queries in log with Rails 4

查看:36
本文介绍了使用 Rails 4 在日志中显示 SQL 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Ubuntu 14.04 上通过我的终端终结者"和我的外壳鱼壳"使用 Rails 4.0.4 和 Ruby 2.1 和 Thin 1.6.2.

I'm using Rails 4.0.4 with Ruby 2.1 and Thin 1.6.2 on Ubuntu 14.04 through my terminal "Terminator" and my shell "Fish Shell".

当我在开发模式下启动 Rails 服务器时,我的日志中没有 SQL 查询,只加载了 JS 和 HTML 文件.

When I'm launching my Rails server in development mode I don't have the SQL queries in my logs only the JS and HTML files are loaded.

我正在寻找类似的东西:

I'm searching for something like that:

User Load (3.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 3 ORDER BY "users"."id" ASC LIMIT 1
(2.0ms)  SELECT COUNT(*) FROM "users" WHERE (driver_register_state_cd = -2)

推荐答案

rails 控制台从不写入日志文件,但您可以很容易地实现它,例如,如果您在启动 rails 控制台后执行以下操作

the rails console never writes to the log file, but you can achieve it quite easily, for example, if you execute following after starting the rails console

ActiveRecord::Base.logger = Logger.new STDOUT

rails 会将所有 SQL 语句记录到标准输出,从而将它们显示在您的终端中.由于 Logger.new 接受任何流作为第一个参数,您可以让它写入 rails development.log:

rails will log all SQL statements to stdout, thus display them in your terminal. and since Logger.new accepts any stream as first argument, you could just let it write to the rails development.log:

ActiveRecord::Base.logger = Logger.new File.open('log/development.log', 'a')

这篇关于使用 Rails 4 在日志中显示 SQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆