justin․searls․co

Pro-tip: make your debug print statements POP 🍾

This isn't an exciting idea, but since I know a lot of puts debuggerers who are probably accustomed to printing plain text into a sea of log noise, I thought I'd share this PSA: making your print statements visually distinctive makes them easier to spot.

If you're trying to log a value during the request/response lifecycle of a Rails development server, there's a lot of chatter for every page load. So instead of passing something to puts or pp and spraining a squint muscle to find your print statement in the server logs, do something to make it stand out!

You could reach for a gem like awesome_print for this, but IMNSHO there's no need to add another dependency just for this.

Since I use a white-background terminal, my quick solution was to define a method that pretty-prints its argument with a black background (called Kernel#buts). Since I only need this for my development server, I chucked it in a development-only initializer:

# config/initializers/buts.rb

return unless Rails.env.development?

module Kernel
 # Make your output stand out more in the Rails server console
 def buts(obj)
   black_background_white_text = "\e[30;47m"
   reset = "\e[0m"
   puts "#{black_background_white_text}#{obj.pretty_inspect}#{reset}"
 end
end

Here's the before-and-after. At a glance, can you even see what I'm trying to print in the first screenshot?

There you go. Life's too short to be hunting through logs for one-off print statements. 🔎


Got a taste for hot, fresh takes?

Then you're in luck, because you can subscribe to this site via RSS or Mastodon! And if that ain't enough, then sign up for my newsletter and I'll send you a usually-pretty-good essay once a month. I also have a solo podcast, because of course I do.