Why am I getting "warning: this expression will fail with ArgumentError"

In a module I have this:

def something(cmd)
  # .........

  # warning here
  Logger.error("#{__MODULE__}_#{Atom.to_string(__ENV__.function)}: error executing the command: #{cmd}")

  # .........

To that there’s a warning:

  warning: this expression will fail with ArgumentError
1 Like

Because if you examine what __ENV__.function actually returns it is not an atom. It’s a tuple {function_name, arity}.

BTW, the Logger has really good metadata support and the Logger macros (like Logger.error/2 already capture the module and function. You just need to whitelist them in your config.exs for the console backend. For verbose output (might be useful in development) try:

config :logger, :console,
  metadata: :all


config :logger, :console,
  metadata: [:module, :function]

I recommend two good sources for futher reading: