puts vs logger in rails rake tasks

2019-03-08 02:45发布

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?

8条回答
趁早两清
2楼-- · 2019-03-08 03:08

You could create a new rake task to get this to work.

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 some_task
查看更多
迷人小祖宗
3楼-- · 2019-03-08 03:10

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

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 you'd like this behaviour only in the Rails console, place the same block of code in your ~/.irbrc.

查看更多
时光不老,我们不散
4楼-- · 2019-03-08 03:23

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.

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.

(via)

查看更多
女痞
5楼-- · 2019-03-08 03:29

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.

查看更多
Summer. ? 凉城
6楼-- · 2019-03-08 03:29

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

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

To redirect logger in controllers:

ActionController::Base.logger = Logger.new(STDOUT)
查看更多
仙女界的扛把子
7楼-- · 2019-03-08 03:30

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

查看更多
登录 后发表回答