How to make Rails.logger.debug print hash more readable
Asked Answered
U

5

19

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?

Urumchi answered 8/7, 2013 at 17:38 Comment(0)
U
22

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"}
Urumchi answered 8/7, 2013 at 17:46 Comment(0)
A
16

It's even easier to read it when you use to_yaml eg:

logger.debug my_hash.to_yaml

Which is an easy to read format over multiple lines. The inspect method simply spews out a string.

Atheistic answered 11/2, 2015 at 16:37 Comment(1)
Great tip here! 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 with ivars 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_yamlGaikwar
W
3
my_hash = {'a' => 'alligator', 'b'=>'baboon'}
logger.debug "#{my_hash}"

Then, it looks like

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

do not need inspect

Whitcher answered 9/5, 2017 at 13:32 Comment(0)
L
1

There is an another way to do this. There is a ruby built in module pp.rb that is Pretty-printer for Ruby objects.

non-pretty-printed output by p is:

#<PP:0x81fedf0 @genspace=#<Proc:0x81feda0>, @group_queue=#<PrettyPrint::GroupQueue:0x81fed3c @queue=[[#<PrettyPrint::Group:0x81fed78 @breakables=[], @depth=0, @break=false>], []]>, @buffer=[], @newline="\n", @group_stack=[#<PrettyPrint::Group:0x81fed78 @breakables=[], @depth=0, @break=false>], @buffer_width=0, @indent=0, @maxwidth=79, @output_width=2, @output=#<IO:0x8114ee4>>

pretty-printed output by pp is:

#<PP:0x81fedf0
 @buffer=[],
 @buffer_width=0,
 @genspace=#<Proc:0x81feda0>,
 @group_queue=
  #<PrettyPrint::GroupQueue:0x81fed3c
   @queue=
    [[#<PrettyPrint::Group:0x81fed78 @break=false, @breakables=[], @depth=0>],
     []]>,
 @group_stack=
  [#<PrettyPrint::Group:0x81fed78 @break=false, @breakables=[], @depth=0>],
 @indent=0,
 @maxwidth=79,
 @newline="\n",
 @output=#<IO:0x8114ee4>,
 @output_width=2>
Liselisetta answered 6/8, 2017 at 4:0 Comment(1)
This would pretty-print the hash to a console, but how do you send pp output to a log file?Amphictyon
R
1

For more complex objects even with ActiveRecord, this could be achieved with a JSON.pretty_generate

Rails.logger.debug JSON.pretty_generate(my_hash.as_json)
Rancell answered 3/11, 2021 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.