Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

puts statements for debug

When I code, I make quite intense use of "puts" statements for debugging. It allows me to see what happens in the server.

When the code is debugged, I use to remove these "puts" statements for I don't know what reason.

Is it a good idea or should I leave them instead to give more clarity to my server logs?

like image 292
ndemoreau Avatar asked Sep 06 '11 07:09

ndemoreau


2 Answers

You should use the logger instead of puts. Use this kind of statements:

Rails.logger.debug "DEBUG: #{self.inspect} #{caller(0).first}" if Rails.logger.debug?

If you want to see the debugging in the real-time (almost), just use the tail command in another terminal window:

tail -F log/development.log | grep DEBUG

Then you do not need to remove these statements in production, and they will not degrade performance too much, because if logger.debug? will prevent the (possibly expensive) construction of the message string.

Using standard output for debugging is usually a bad practice. In cases you need such debug, use the diagnostic STDERR, as in:

STDERR.puts "DEBUG: xyzzy"

Most of the classes in Rails (models, controllers and views) have the method logger, so if possible use it instead of the full Rails.logger. If you are using older versions of Rails, use the constant RAILS_DEFAULT_LOGGER instead of Rails.logger.

like image 190
Arsen7 Avatar answered Sep 17 '22 10:09

Arsen7


use the logger : http://guides.rubyonrails.org/debugging_rails_applications.html#the-logger

like image 21
m_x Avatar answered Sep 17 '22 10:09

m_x