When debugging in Elixir, I often use IO.inspect/2
to print the contents of a variable:
whatever
|> IO.inspect()
To improve readability, I usually add a label and format the output nicely:
whatever
|> IO.inspect(label: "Feeds", pretty: true)
However, this still prints plain text, without any syntax highlighting.
To enable colors in the terminal, you can pass the :syntax_colors
option with IO.ANSI.syntax_colors/0
:
whatever
|> IO.inspect(label: "Feeds", pretty: true, syntax_colors: IO.ANSI.syntax_colors())
This renders the output with ANSI color codes, making structured data much easier to read—especially maps and structs.
If you're using this often, consider creating a helper function:
defmodule Debug do
def print(term, label \\ "Debug") do
IO.inspect(term, label: label, pretty: true, syntax_colors: IO.ANSI.syntax_colors())
end
end
Then use it like this:
Debug.print(whatever, "Feeds")
Now your debug output is both informative and visually pleasant.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.