Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails puts to console

In my Rails app, there are some cases where code is being outputted with the puts command (for debugging purposes). Is there anyway to follow this output in the rails console (with the rails c command)? Or is there any other way to debug/view logs in the rails console?

Thanks!

like image 560
Flock Dawson Avatar asked Feb 17 '26 23:02

Flock Dawson


2 Answers

For debugging, use Rails.logger instead of puts. For example:

Rails.logger.info "Some debugging info"

This will be logged to a log file in rails_app_root/log directory. If you are running in development environment locally, it will be logged to rails_app/log/development.log file.

Now, to see the log as they come in you can use tail command, like this:

tail -f log/development.log

Hope it helps.

like image 133
andHapp Avatar answered Feb 19 '26 13:02

andHapp


Rails.logger is the best solution,one more think i want to add,if you want separate log file you could use like this

def read(args)    
 unless args.blank?
   cache_key= self.get_cache_key(args)
 end
 logger = Logger.new("#{Rails.root}/log/cache_read.log")
 logger.error("cache read scope == #{cache_key.to_s}")
end

so cache_read.log file having only this method log only.

like image 27
Jenorish Avatar answered Feb 19 '26 11:02

Jenorish