在控制台中禁用 Rails SQL 日志记录 [英] Disable Rails SQL logging in console

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

问题描述

当我在控制台中执行命令时,有没有办法禁用 SQL 查询日志记录?理想情况下,如果我可以禁用它并使用控制台中的命令重新启用它,那就太好了.

Is there a way to disable SQL query logging when I'm executing commands in the console? Ideally, it would be great if I can just disable it and re-enable it with a command in the console.

我正在尝试调试某些内容并使用puts"打印出一些相关数据.但是,sql 查询输出使其难以阅读.

I'm trying to debug something and using "puts" to print out some relevant data. However, the sql query output is making it hard to read.

我找到了另一个解决方案,因为将记录器设置为 nil 有时会引发错误,如果不是我的代码尝试调用 logger.warn

I found another solution, since setting the logger to nil sometimes raised an error, if something other than my code tried to call logger.warn

您可以将记录器的级别设置为 1,而不是将记录器设置为 nil.

Instead of setting the logger to nil you can set the level of the logger to 1.

ActiveRecord::Base.logger.level = 1 # or Logger::INFO

推荐答案

要关闭它:

old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil

重新开启:

ActiveRecord::Base.logger = old_logger

这篇关于在控制台中禁用 Rails SQL 日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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