将 vs 记录器放入 Rails rake 任务中
在 rake 任务中,如果我使用 put 命令,那么我会在控制台上看到输出。但是,当应用程序部署在生产环境中时,我不会在日志文件中看到该消息。
但是,如果我说 Rails.logger.info 那么在开发模式下我在控制台上看不到任何内容。我需要去日志文件并跟踪它。
理想情况下,我希望使用 Rails.logger.info 并在 rake 任务内的开发模式下,记录器的输出也应发送到控制台。
有办法实现吗?
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.
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
将其放入
application.rb
中,或放在 rake 任务初始化代码中这是 Rails 3 代码。请注意,这将覆盖
development.log
的日志记录。如果您想要STDOUT
和development.log
您将需要一个包装函数。如果您只想在 Rails 控制台中实现此行为,请将相同的代码块放入
~/.irbrc
中。Put this in
application.rb
, or in a rake task initialize codeThis is Rails 3 code. Note that this will override logging to
development.log
. If you want bothSTDOUT
anddevelopment.log
you'll need a wrapper function.If you'd like this behaviour only in the Rails console, place the same block of code in your
~/.irbrc
.您可以创建一个新的 rake 任务来使其发挥作用。
这样,当您执行 rake 任务时,您可以首先添加 to_stdout 以获取 stdout 日志消息,或者不包含它以将消息发送到默认日志文件
You could create a new rake task to get this to work.
This way when you execute your rake task you can add to_stdout first to get stdout log messages or don't include it to have messages sent to the default log file
对于 Rails 4 及更高版本的代码
,您可以使用 Logger 广播。
如果您想在开发模式下获取 rake 任务的 STDOUT 和文件日志记录,可以将此代码添加到 config/environments/development.rb 中:
测试
这是一个小的 Rake 任务来测试上述代码:
运行
rake stdout_and_log:test
输出已
添加到
log/development.log
中。运行
rake stdout_and_log:test RAILS_ENV=product
输出已
添加到
log/production.log
中。Code
For Rails 4 and newer, you can use Logger broadcast.
If you want to get both STDOUT and file logging for rake tasks in development mode, you can add this code into
config/environments/development.rb
:Test
Here's a small Rake task to test the above code :
Running
rake stdout_and_log:test
outputswhile
has been added to
log/development.log
.Running
rake stdout_and_log:test RAILS_ENV=production
outputswhile
has been added to
log/production.log
.Rake 任务由用户在命令行上运行。他们需要立即了解的任何内容(“已处理的 5 行”)都应该使用
puts
在终端上输出。需要为后代保留的任何内容(“已向 [电子邮件受保护] 发送警告电子邮件") 应发送到
Rails.logger
。Rake tasks are run by a user, on a command-line. Anything they need to know right away ("processed 5 rows") should be output on the terminal with
puts
.Anything that needs to be kept for posterity ("sent warning email to [email protected]") should be sent to the
Rails.logger
.我想说使用 Rails.logger.info 是正确的方法。
您将无法在服务器控制台中看到它,因为它不会通过服务器运行。只需打开一个新控制台并
tail -f
日志文件,就可以了。(来自)
I'd say that using
Rails.logger.info
is the way to go.You won't be able to see it in the server console because it won't run via the server. Just open up a new console and
tail -f
the log file, it'll do the trick.(via)
在 Rails 2.X 中,将记录器重定向到模型中的 STDOUT:
重定向控制器中的记录器:
In Rails 2.X to redirect the logger to STDOUT in models:
To redirect logger in controllers:
使用“&”执行后台作业并打开脚本/控制台或其他..
这样您就可以在同一窗口中运行多个命令。
注意 当有大量日志输出时,可能会很快变得马虎。
Execute a background job with '&' and open script/console or whatever..
That way you can run multiple commands in the same window.
note Can get sloppy quickly when there is a lot of logging output.
创建一个应用程序助手来检测正在运行的环境并执行正确的操作怎么样?
然后调用output_debug而不是puts或logger.info
How about creating an application helper which detects which environment is running and does the right thing?
Then call output_debug instead of puts or logger.info
扩展@Eric Duminil 的答案。我们可以使用rails提供的
rake_tasks
钩子仅在调用rake任务时运行代码块,即当服务器或控制台运行时该块不会运行。输出:
Extending on @Eric Duminil's answer. We can use use
rake_tasks
hook provided by rails to run code block only when rake tasks are invoked, i.e. this block will not run when server or console are run.Output: