在 rake 任务期间记录 SQL 查询 [英] Log SQL queries during rake tasks

查看:53
本文介绍了在 rake 任务期间记录 SQL 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于打印每个执行的 SQL 查询的rails 服务器",我想对 rake 任务执行相同的操作.

Similar to 'rails server' that prints every SQL query executed I would like to do the same for rake tasks.

实现这一目标的最佳方法是什么?

What is the best way to achieve that?

推荐答案

根据您的环境,Rake 将记录 sql 查询,就像任何 Rails 进程将 &在同一个日志文件中.因此,在您的开发框中,检查您的 log/development.log 文件 - 它将包含您的 Rake 任务的查询.如果您希望在生产环境中记录查询,请将 Rake 任务中的日志级别设置为 DEBUG,并确保 rake 任务依赖于 :environment.

Depending on your environment, Rake will log sql queries just like any Rails process will & in the same logfile. So on your dev box, check your log/development.log file - it will contain your Rake task's queries. If you want queries logged in production, set the log level in your Rake task to DEBUG, and make sure the rake task depends on :environment.

desc "Task with SQL logging"
task :test_log => :environment do
  Rails.logger.level = Logger::DEBUG
  Your code here...
end

这篇关于在 rake 任务期间记录 SQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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