Well this is embarrassing. Logger vs IO.inspect

I was today years old when I learned to use Logger.notice instead of IO.inspect everywhere that I wanted to peek at what was going on with the state of my application. Maybe dialyzer or mix could suggest that instead of just warning “There should be no calls to inspect.”

2 Likes

Credo has such warning.

3 Likes

Logger.notice compared to IO.inspect is configurable, you have log levels and you can disable logs altogether. You don’t want to have random inspects in your production code.

1 Like

I don’t think the two are equivalent… both are fruit, but apples and oranges?

notice indicates the severity of the message logged – if you have a logging threshold of info, for example, debug messages won’t show up but notices will. The syslog protocol defines the available severities with numerical codes (see section 6.2.1 table 2 on page 11). A “notice” is described as “normal but significant condition” and it lives right between “info” and “warning”.

I’m not an expert on this, but Logger sends messages buffered to specific targets whereas IO.inspect is pegged to STDOUT. Sometimes I have to remember this e.g. if I start a process in one iex session and when I join it via another Terminal window. IO.inspect may be visible in one and not the other.

4 Likes

Very much this. IO.inspect is the printf of elixir, while Logger is a means of logging to configurable backends with multiple capabilities besides raw string logs, …. The stuff you’d pull in a library for in many languages.

2 Likes

Thanks for the explanations. I understand they are not equivalent to or exclusionary of one another. I just was ONLY using IO.inspect even in situations that Logger seems much more fitting now that I know about it.

You might find this library useful to inspect using Logger: untangle | Hex

2 Likes