将 vs 记录器放入 rails rake 任务中 [英] puts vs logger in rails rake tasks

查看:43
本文介绍了将 vs 记录器放入 rails rake 任务中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 rake 任务中,如果我使用 puts 命令,那么我会在控制台上看到输出.但是,当应用程序部署到生产环境时,我不会在日志文件中看到该消息.

In a rake task if I use puts command then I see the output on console. However I will not see that message in log file when app is deployed on production.

但是,如果我说 Rails.logger.info,那么在开发模式下,我在控制台上什么也看不到.我需要去日志文件并跟踪它.

However if I say Rails.logger.info then in development mode I see nothing on console. I need to go to log file and tail that.

我希望在 rake 任务中使用 Rails.logger.info,并且在开发模式下,记录器的输出也应该发送到控制台.

I would ideally like to use Rails.logger.info and in development mode inside the rake task, the output from logger should also be sent to console.

有没有办法做到这一点?

Is there a way to achieve that?

推荐答案

把它放在 application.rb 或 rake 任务初始化代码中

Put this in application.rb, or in a rake task initialize code

if defined?(Rails) && (Rails.env == 'development')
  Rails.logger = Logger.new(STDOUT)
end

这是 Rails 3 代码.请注意,这将覆盖对 development.log 的日志记录.如果您同时需要 STDOUTdevelopment.log,则需要一个包装函数.

This is Rails 3 code. Note that this will override logging to development.log. If you want both STDOUT and development.log you'll need a wrapper function.

如果您只希望在 Rails 控制台中执行此操作,请将相同的代码块放在您的 ~/.irbrc 中.

If you'd like this behaviour only in the Rails console, place the same block of code in your ~/.irbrc.

这篇关于将 vs 记录器放入 rails rake 任务中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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