Elixir: Logger and dialyzer

Created on 6 Apr 2016  路  5Comments  路  Source: elixir-lang/elixir

  • Elixir version (elixir -v): Elixir 1.2.4
  • Operating system: OS X 10.11.4

When configured to check unmatched returns, dialyzer complains about each usage of Logger: Expression produces a value of type 'ok' | {'error',_}, but this value is unmatched.
It's possible to overcome this problem by typing :ok = Logger.info "....", but it's cumbersome.

Static analysis is the key for a robust system ("failing fast" doesn't mean to have a badly written code :-)). On the one hand I don't want to deactivate this warning, but on the other hand keeping it means that I will possibly miss an important error among all warnings reported for usages of Logger.

Maybe something simple as Logger.info! could do the trick?

All 5 comments

The slippery slope with regard to ! is that you may (and _often_ I would add) want to just log and don't care if it fails or goes well. :ok = Logger.info(...) has a specific semantic: it will fail with a MatchError if Logger.info/2 returns {:error, _}. For this reason, we can't just go our merry way and change all Logger calls to match :ok, as this is not the semantics they currently have.

I think the dialyzer warning can be solved without changing the semantics by doing this:

_ = Logger.info(...)

Yes, indeed it works, but between :ok = Logger.info and _ = Logger.info, there's not so much difference (although the first one can fail). But it still feels awkward, just for logging, no ?

@ahamez I feel like failing or not failing is quite a huge difference! :) I don't think crashing the process if logging fails is an acceptable behaviour in many (if not most) applications.

If you are using dialyzer, then you should explicitly handle its requirements. That's going to be preferable than hiding the warnings behind Logger.info!, specially because, as @whatyouhide side, you likely want to use _ = Logger.info.

OK, I'll go the _ = Logger.... way, even though it feels awkward just for logging. Maybe I'll come up with some macro to hide this.

Was this page helpful?
0 / 5 - 0 ratings