将vs日志记录器放在rails rake任务中

时间:2022-05-31 22:28:29

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任务中,如果我使用put命令,那么我将看到控制台的输出。但是,当应用程序部署到产品中时,我将不会在日志文件中看到该消息。

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,那么在开发模式中,我在控制台什么也看不到。我需要去log file并跟踪它。

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.

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

Is there a way to achieve that?

是否有办法做到这一点?

8 个解决方案

#1


49  

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

把这个应用程序。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 3代码。注意,这会将日志覆盖到development.log。如果你想要STDOUT和development。您将需要一个包装器函数。

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

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

#2


28  

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

#3


10  

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行”)都应该在带有put的终端上输出。

Anything that needs to be kept for posterity ("sent warning email to jsmith@example.com") should be sent to the Rails.logger.

任何需要保存给后代的东西(“发送警告邮件到jsmith@example.com”)都应该发送到Rails.logger。

#4


9  

I'd say that using Rails.logger.info is 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 -f the 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.

许多用户都知道UNIX®命令“尾巴”,可以用来显示一个大文件的最后几行。这对于查看日志文件等非常有用。

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.

在某些情况下更有用的是“-f”参数到“tail”命令。这会导致tail跟踪文件的输出。最初,响应将与“tail”本身相同——文件的最后几行将显示出来。但是,该命令不会返回到提示符,而是继续“跟踪”该文件。当附加的行被添加到文件中时,它们将显示在终端上。这对于查看日志文件或随时间而添加的其他文件非常有用。输入“man tail”,了解更多关于这个和其他tail选项的细节。

(via)

(通过)

#5


5  

Code

For Rails 4 and newer, you can use Logger broadcast.

对于Rails 4和更新版本,您可以使用日志记录器广播。

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和文件日志记录,您可以将这些代码添加到配置/环境/开发中。rb:

  if File.basename($0) == 'rake'
    # http://*.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任务来测试上面的代码:

# 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

Running rake stdout_and_log:test outputs

运行rake stdout_and_log:测试输出

HELLO FROM PUTS
HELLO FROM LOGGER

while

HELLO FROM LOGGER

has been added to log/development.log.

已添加到日志/开发。日志。

Running rake stdout_and_log:test RAILS_ENV=production outputs

运行rake stdout_and_log:test RAILS_ENV=生产输出

HELLO FROM PUTS

while

HELLO FROM LOGGER

has been added to log/production.log.

已添加到log/production.log。

#6


4  

How about creating an application helper which detects which environment is running and does the right thing?

如何创建一个应用程序助手来检测正在运行的环境并执行正确的操作?

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

Then call output_debug instead of puts or logger.info

然后调用output_debug而不是put或logger.info

#7


3  

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

Rails 2。X将日志记录器重定向到模型中的STDOUT:

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

To redirect logger in controllers:

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

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

#8


1  

Execute a background job with '&' and open script/console or whatever.. That way you can run multiple commands in the same window.

使用'&'和打开脚本/控制台或其他工具执行后台作业。这样,您就可以在同一个窗口中运行多个命令。

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 Can get sloppy quickly when there is a lot of logging output.

注意,当有大量日志输出时,会很快变得草率。

#1


49  

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

把这个应用程序。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 3代码。注意,这会将日志覆盖到development.log。如果你想要STDOUT和development。您将需要一个包装器函数。

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

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

#2


28  

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

#3


10  

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行”)都应该在带有put的终端上输出。

Anything that needs to be kept for posterity ("sent warning email to jsmith@example.com") should be sent to the Rails.logger.

任何需要保存给后代的东西(“发送警告邮件到jsmith@example.com”)都应该发送到Rails.logger。

#4


9  

I'd say that using Rails.logger.info is 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 -f the 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.

许多用户都知道UNIX®命令“尾巴”,可以用来显示一个大文件的最后几行。这对于查看日志文件等非常有用。

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.

在某些情况下更有用的是“-f”参数到“tail”命令。这会导致tail跟踪文件的输出。最初,响应将与“tail”本身相同——文件的最后几行将显示出来。但是,该命令不会返回到提示符,而是继续“跟踪”该文件。当附加的行被添加到文件中时,它们将显示在终端上。这对于查看日志文件或随时间而添加的其他文件非常有用。输入“man tail”,了解更多关于这个和其他tail选项的细节。

(via)

(通过)

#5


5  

Code

For Rails 4 and newer, you can use Logger broadcast.

对于Rails 4和更新版本,您可以使用日志记录器广播。

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和文件日志记录,您可以将这些代码添加到配置/环境/开发中。rb:

  if File.basename($0) == 'rake'
    # http://*.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任务来测试上面的代码:

# 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

Running rake stdout_and_log:test outputs

运行rake stdout_and_log:测试输出

HELLO FROM PUTS
HELLO FROM LOGGER

while

HELLO FROM LOGGER

has been added to log/development.log.

已添加到日志/开发。日志。

Running rake stdout_and_log:test RAILS_ENV=production outputs

运行rake stdout_and_log:test RAILS_ENV=生产输出

HELLO FROM PUTS

while

HELLO FROM LOGGER

has been added to log/production.log.

已添加到log/production.log。

#6


4  

How about creating an application helper which detects which environment is running and does the right thing?

如何创建一个应用程序助手来检测正在运行的环境并执行正确的操作?

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

Then call output_debug instead of puts or logger.info

然后调用output_debug而不是put或logger.info

#7


3  

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

Rails 2。X将日志记录器重定向到模型中的STDOUT:

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

To redirect logger in controllers:

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

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

#8


1  

Execute a background job with '&' and open script/console or whatever.. That way you can run multiple commands in the same window.

使用'&'和打开脚本/控制台或其他工具执行后台作业。这样,您就可以在同一个窗口中运行多个命令。

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 Can get sloppy quickly when there is a lot of logging output.

注意,当有大量日志输出时,会很快变得草率。