I'm using Rails.logger.debug print variables for debugging purposes. The issue is it prints hashes in an impossible to read format (can't distinguish keys from values). For example, I add the following lines to my code base
#code_base.rb
my_hash = {'a' => 'alligator', 'b'=>'baboon'}
Rails.logger.debug my_hash
Then I launch my rails app and type
tail -f log/development.log
But when my_hash gets printed, it looks like
bbaboonaalligator
The key and values are scrunched up, making it impossible to parse. Do you guys know what I should do to fix this?
to_yaml
also creates a string, but it's formatted with\n
and indentation so it's much easier to skim a large or nested hash… If you're trying to log a hash-like object (say,params
), you may end up withivars
clogging the data you're interested in. Turn it into a normal hash first and then turn that into yaml:logger.debug params.to_hash.to_yaml
– Gaikwar