Debugging a pipeline with IO.inspect/2

Did you know that IO.inspect/2 returns the the first argument and accepts a label option as a second argument. This makes it a perfect tool to logging intermediate results in a pipeline, as well as intermediate results in a function. Take the following pipeline, for example:

def get_gifts(santa, wishlist, snack) do
  santa
  |> receives_wishlist(wishlist)
  |> leaves_presents
  |> eats(snack)
  |> wakeup_to
end

Now lets say that you don’t get your gifts. Try the following:

def get_gifts(santa, wishlist, snack) do
  santa
  |> receives_wishlist(wishlist)
  |> IO.inspect(label: "after wishlist received")
  |> leaves_presents()
  |> IO.inspect(label: "after presents")
  |> eats(snack)
  |> wakeup_to()
  |> IO.inspect(label: "result")
end
6 Likes

Indeed a very nice feature! And on the issue tracker there is even a discussion about making this possible for Logger calls as well…

1 Like

I think there is a bug in your code. You should pattern match for :naughty or :nice or something. That’s why the gift doesn’t get delivered.

Sorry for the offtopic. I couldn’t resist. :smiley:

2 Likes

I would love to see it implemented for logger too. I’ve been burned a couple times with at :ok return value.

2 Likes