How to make Rails.logger.debug print hash more readable

User314159 picture User314159 · Jul 8, 2013 · Viewed 14.2k times · Source

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?

Answer

User314159 picture User314159 · Jul 8, 2013

Nevermind, I found the answer to my own question. I need to use

my_hash = {'a' => 'alligator', 'b'=>'baboon'}
Rails.logger.debug "#{my_hash.inspect}"

Then, it looks like

{"b"=>"baboon", "a"=>"aligator"}