Ruby-on-rails 将 vs 记录器放入 rails rake 任务中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2246141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
puts vs logger in rails rake tasks
提问by Nick Vanderbilt
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.
在 rake 任务中,如果我使用 puts 命令,那么我会在控制台上看到输出。但是,当应用程序部署到生产中时,我不会在日志文件中看到该消息。
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.
但是,如果我说 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.
理想情况下,我想在 rake 任务中使用 Rails.logger.info 和开发模式,记录器的输出也应该发送到控制台。
Is there a way to achieve that?
有没有办法实现这一目标?
回答by shmichael
Put this in application.rb, or in a rake task initialize code
将其放入application.rb, 或 rake 任务初始化代码中
if defined?(Rails) && (Rails.env == 'development')
Rails.logger = Logger.new(STDOUT)
end
This is Rails 3 code. Note that this will override logging to development.log. If you want both STDOUTand development.logyou'll need a wrapper function.
这是 Rails 3 代码。请注意,这将覆盖日志记录到development.log. 如果你想同时STDOUT和development.log你需要的包装功能。
If you'd like this behaviour only in the Rails console, place the same block of code in your ~/.irbrc.
如果您只希望在 Rails 控制台中执行此操作,请将相同的代码块放在~/.irbrc.
回答by Pete Brumm
You could create a new rake task to get this to work.
你可以创建一个新的 rake 任务来让它工作。
desc "switch logger to stdout"
task :to_stdout => [:environment] do
Rails.logger = Logger.new(STDOUT)
end
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
这样,当您执行 rake 任务时,您可以先添加 to_stdout 以获取 stdout 日志消息,或者不包含它以将消息发送到默认日志文件
rake to_stdout some_task
回答by Jonathan Julian
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.
Rake 任务由用户在命令行上运行。他们需要立即知道的任何内容(“已处理 5 行”)都应在终端上输出puts.
Anything that needs to be kept for posterity ("sent warning email to [email protected]") should be sent to the Rails.logger.
任何需要为后代保留的内容(“向 [email protected] 发送警告电子邮件”)都应发送到Rails.logger.
回答by marcgg
I'd say that using Rails.logger.infois the way to go.
我会说使用Rails.logger.info是要走的路。
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 -fthe log file, it'll do the trick.
您将无法在服务器控制台中看到它,因为它不会通过服务器运行。只需打开一个新的控制台和tail -f日志文件,它就可以解决问题。
Many users are aware of the UNIX? command 'tail', which can be used to display the last few lines of a large file. This can be useful for viewing log files, etc.
Even more useful in some situations, is the '-f' parameter to the 'tail' command. This causes tail to 'follow' the output of the file. Initially, the response will be the same as for 'tail' on its own - the last few lines of the file will be displayed. However, the command does not return to the prompt, and instead, continues to 'follow' the file. When additional lines are added to the file, they will be displayed on the terminal. This is very useful for watching log files, or any other file which may be appended over time. Type 'man tail' for more details on this and other tail options.
很多用户都知道UNIX?命令'tail',可用于显示大文件的最后几行。这对于查看日志文件等很有用。
在某些情况下更有用的是“tail”命令的“-f”参数。这会导致 tail “跟随”文件的输出。最初,响应将与 'tail' 本身相同 - 将显示文件的最后几行。但是,该命令不会返回到提示,而是继续“跟随”文件。当额外的行被添加到文件中时,它们将显示在终端上。这对于查看日志文件或可能随时间附加的任何其他文件非常有用。键入“man tail”以获取有关此选项和其他尾选项的更多详细信息。
(via)
(通过)
回答by Eric Duminil
Code
代码
For Rails 4 and newer, you can use Logger broadcast.
对于 Rails 4 和更新版本,您可以使用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:
如果您想在开发模式下获得 rake 任务的 STDOUT 和文件日志记录,您可以将此代码添加到config/environments/development.rb:
if File.basename(# lib/tasks/stdout_and_log.rake
namespace :stdout_and_log do
desc "Test if Rails.logger outputs to STDOUT and log file"
task :test => :environment do
puts "HELLO FROM PUTS"
Rails.logger.info "HELLO FROM LOGGER"
end
end
) == 'rake'
# http://stackoverflow.com/questions/2246141/puts-vs-logger-in-rails-rake-tasks
log_file = Rails.root.join("log", "#{Rails.env}.log")
Rails.logger = ActiveSupport::Logger.new(log_file)
Rails.logger.extend(ActiveSupport::Logger.broadcast(ActiveSupport::Logger.new(STDOUT)))
end
Test
测试
Here's a small Rake task to test the above code :
这是一个小的 Rake 任务来测试上面的代码:
HELLO FROM PUTS
HELLO FROM LOGGER
Running rake stdout_and_log:testoutputs
运行rake stdout_and_log:test输出
HELLO FROM LOGGER
while
尽管
HELLO FROM PUTS
has been added to log/development.log.
已添加到log/development.log.
Running rake stdout_and_log:test RAILS_ENV=productionoutputs
运行rake stdout_and_log:test RAILS_ENV=production输出
HELLO FROM LOGGER
while
尽管
def output_debug(info)
if RAILS_ENV == "development"
puts info
else
logger.info info
end
end
has been added to log/production.log.
已添加到log/production.log.
回答by naven87
How about creating an application helper which detects which environment is running and does the right thing?
如何创建一个应用程序助手来检测正在运行的环境并做正确的事情?
ActiveRecord::Base.logger = Logger.new(STDOUT)
Then call output_debug instead of puts or logger.info
然后调用 output_debug 而不是 puts 或 logger.info
回答by Tania R
In Rails 2.X to redirect the logger to STDOUT in models:
在 Rails 2.X 中,将记录器重定向到模型中的 STDOUT:
ActionController::Base.logger = Logger.new(STDOUT)
To redirect logger in controllers:
在控制器中重定向记录器:
tail -f log/development.log &
script/console
Loading development environment (Rails 2.3.5)
>> Product.all
2011-03-10 11:56:00 18062 DEBUG Product Load (6.0ms) SELECT * FROM "products"
[<Product.1>,<Product.2>]
回答by Tom Maeckelberghe
Execute a background job with '&' and open script/console or whatever.. That way you can run multiple commands in the same window.
使用“&”执行后台作业并打开脚本/控制台或其他任何东西.. 这样您就可以在同一个窗口中运行多个命令。
##代码##noteCan get sloppy quickly when there is a lot of logging output.
注意当有大量日志输出时,可能会很快变得草率。

