在某些情况下更有用,
是‘ tail’的’-f’参数
这会导致 tail“ follow”
文件的输出
响应将与... 相同
“ tail”本身-最后几行
将显示该文件的。
However, the command does not return
转到提示符,而是继续
“跟踪”该文件。当额外的
行添加到文件中时,它们将
be displayed on the terminal. This is
very useful for watching log files, or
可以附加的任何其他文件
随着时间的推移。输入“男性尾巴”为更多
关于这个和其他尾巴的细节
options.
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 jsmith@example.com") should be sent to the Rails.logger.
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 STDOUT and development.log you'll need a wrapper function.
if File.basename($0) == '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
测试
下面是一个测试上面代码的小 Rake 任务:
# 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