I have a rake task that gets run as a cron job once a day. It performs some maintenance on a model that currently has 57k+ records in it. This adds hundreds of thousands of lines to the log each time the task is run for whichever environment it's currently in. (Currently it's just in development.)
我有一个耙子任务,每天作为cron任务运行一次。它对当前拥有57k+记录的模型执行一些维护。这将在每次运行任务时向日志中添加成千上万条行,以查看当前所处的环境。(目前还在开发中。)
How can I disable logging for a specific rake task or group of tasks, but leave logging alone for the rest of the app and leave it alone for the model/methods that are called from the task?
如何禁用特定rake任务或任务组的日志记录,而将日志记录留给应用程序的其余部分,并将其留给从任务调用的模型/方法?
3 个解决方案
#1
29
The Rails logger is an ActiveSupport::BufferedLogger
object, but can be re-assigned to any other kind of Logger
object (or any object that responds to the Logger
methods) that you wish, including something like this:
Rails日志记录器是一个ActiveSupport: BufferedLogger对象,但是可以重新分配给您希望的任何其他类型的日志记录器对象(或响应日志记录器方法的任何对象),包括如下内容:
dev_null = Logger.new("/dev/null")
Rails.logger = dev_null
ActiveRecord::Base.logger = dev_null
Put these lines at the top of your Rake task and all logging output will be inexpensively sent to /dev/null
, which is best explained by its Wikipedia article.
将这些行放在Rake任务的顶部,所有日志输出都将以低成本发送到/dev/null,这在Wikipedia文章中得到了最好的解释。
#2
3
If you have a cron job, you can redirect output to /dev/null like :
如果有cron作业,可以将输出重定向到/dev/null:
* * * * * rake whatever_task_blah_blah > /dev/null 2>&1
#3
2
You can add this to the top of the task:
你可以把它添加到任务的顶部:
ActiveRecord::Base.logger.level = 2
#1
29
The Rails logger is an ActiveSupport::BufferedLogger
object, but can be re-assigned to any other kind of Logger
object (or any object that responds to the Logger
methods) that you wish, including something like this:
Rails日志记录器是一个ActiveSupport: BufferedLogger对象,但是可以重新分配给您希望的任何其他类型的日志记录器对象(或响应日志记录器方法的任何对象),包括如下内容:
dev_null = Logger.new("/dev/null")
Rails.logger = dev_null
ActiveRecord::Base.logger = dev_null
Put these lines at the top of your Rake task and all logging output will be inexpensively sent to /dev/null
, which is best explained by its Wikipedia article.
将这些行放在Rake任务的顶部,所有日志输出都将以低成本发送到/dev/null,这在Wikipedia文章中得到了最好的解释。
#2
3
If you have a cron job, you can redirect output to /dev/null like :
如果有cron作业,可以将输出重定向到/dev/null:
* * * * * rake whatever_task_blah_blah > /dev/null 2>&1
#3
2
You can add this to the top of the task:
你可以把它添加到任务的顶部:
ActiveRecord::Base.logger.level = 2