Focused `puts` debugging with STDERR

Alex Taylor
Mar 23, 2018 · 2 min read
Image for post
Image for post
$stderr.puts ftw! Original photo by tidonation on Flickr. (CC 2.0)

I’m a big fan of puts debugging when I’m trying to reproduce a particularly tricky bug, or I want to quickly get a sense of the code path of some action. Sprinkling log lines throughout your code is pretty low-tech, but extremely effective! (As long as it doesn’t end up in master, right? 😉)

This method of debugging becomes especially useful when examining something below your own code, in a gem for instance. You can use bundle open <gem> to open the gem code in your editor, then stuff those puts in there! (Sidenote: once you’re finished with your hackery you can use gem pristine <gem> to undo all your changes.)

One thing that can get in the way of this style of debugging is the other log messages on your console. By default, puts outputs to STDOUT, along with… well, pretty much everything else on a running development server. If you’re using Rails, your little line of debug logs are probably going to get buried in an avalanche of SQL, request/response logging, etc.

The other day I thought of a quick way of making your debug lines stand out without going through too much effort: use STDERR!

A lot of command line programs take advantage of STDERR to separate errors from output. But most web servers will just output all their logs to STDOUT by default.

This is good for us, because we can put the stuff we care about on STDERR…

def my_method(*args)
$stderr.puts "Hello from #my_method!"

# ...
end

… and temporarily silence STDOUT, like the Solo button on a mixing board:

bin/rails server 1>/dev/null

For those not familiar, 1>/dev/null will redirect a program’s STDOUT to /dev/null, the glorious black hole of silence. All you’ll see when running your server will be output from STDERR, which is most likely just your own lines.

I found this to be a really useful way of focusing in on your log messages while you’re troubleshooting. I hope it helps you too!

Also, since I’m talking about puts debugging, I would be remiss if I didn’t mention Aaron Patterson’s excellent post on the topic. puts debugging for life.

Ruby Inside

Alex Taylor

Written by

Alex Taylor is a Senior Software Developer at Clio. He has a passion for Ruby, loves tea, and rides his bike (almost) everywhere.

Ruby Inside

Ruby articles and posts

Alex Taylor

Written by

Alex Taylor is a Senior Software Developer at Clio. He has a passion for Ruby, loves tea, and rides his bike (almost) everywhere.

Ruby Inside

Ruby articles and posts

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store