Search code examples
ruby-on-railsdebuggingloggingpretty-print

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


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?


Solution

  • 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"}