在 rails rake tasks 任务中使用 puts 和 logger 的区别

在 rake 任务中,如果我使用 put 命令,就会在控制台上看到输出。然而,当应用程序部署到生产环境中时,我不会在日志文件中看到这条消息。

然而,如果我说 rails.logger.info ,那么在开发模式下,我在控制台上看不到任何东西。我需要进入日志文件并跟踪它。

理想情况下,我希望在 rake 任务中使用 rails.logger.info 和开发模式,logger 的输出也应该发送到控制台。

有办法做到吗?

66175 次浏览

我认为使用 Rails.logger.info是正确的选择。

您将无法在服务器控制台中看到它,因为它不会通过服务器运行。只要打开一个新的控制台和 tail -f日志文件,就可以了。

许多用户都知道 UNIX 命令‘ tail’,该命令可用于 显示一个大的 这对于查看 日志文件等。

在某些情况下更有用, 是‘ 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.

如何创建一个应用程序帮助器来检测哪个环境正在运行并做正确的事情?

def output_debug(info)
if RAILS_ENV == "development"
puts info
else
logger.info info
end
end

然后调用 output _ debug,而不是 put 或 logger.info

将其放入 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 STDOUT and development.log you'll need a wrapper function.

如果您希望只在 Rails 控制台中实现这种行为,请将相同的代码块放在 ~/.irbrc中。

使用“ &”执行后台作业,并打开脚本/控制台或其他东西。 这样就可以在同一个窗口中运行多个命令。

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>]

当有大量日志输出时,note 可能会很快变得草率。

In Rails 2.X to redirect the logger to STDOUT in models:

ActiveRecord::Base.logger = Logger.new(STDOUT)

在控制器中重定向日志记录器:

ActionController::Base.logger = Logger.new(STDOUT)

您可以创建一个新的 rake 任务来使其工作。

desc "switch logger to stdout"
task :to_stdout => [:environment] do
Rails.logger = Logger.new(STDOUT)
end

这样,当您执行 rake 任务时,您可以首先添加到 _ stdout 来获取标准输出日志消息,或者不包含它来将消息发送到默认的日志文件

rake to_stdout some_task

Code

对于 Rails 4和更新的版本,您可以使用 伐木广播

如果您希望在开发模式下获得 rake 任务的 STDOUT 和文件日志记录,可以将以下代码添加到 config/environments/development.rb:

  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

运行 rake stdout_and_log:test输出

HELLO FROM PUTS
HELLO FROM LOGGER

同时

HELLO FROM LOGGER

已被添加到 log/development.log

运行 rake stdout_and_log:test RAILS_ENV=production输出

HELLO FROM PUTS

同时

HELLO FROM LOGGER

已被添加到 log/production.log