Skip to content

Logger functions should accept more than just maps or list as metadata #14691

@ponychicken

Description

@ponychicken

Elixir and Erlang/OTP versions

I believe that for beginners its always good when handling errors does not involve any gotchas.
A common way of how someone coming from other languages might attempt to catch and report an error would be something like this:

    try do
      result = do_something_complicated()
      {:ok, result}
    rescue
      e ->
        Logger.error("Failed to do something", e)
        {:error, :db_error}
    end

Unfortunately the Logger.error/2 (and all related defs) only accept Maps and List as metadata. So this attempt will just throw it self an (Protocol.UndefinedError) protocol Enumerable not implemented for type RuntimeError (a struct). error, which can be frustrating.

I believe it would be desirable for the Logger functions to be able to whatever metadata you throw at them, or at least structs. Of course we can use inspect(e) inside the message, but this feels like unneeded boilerplate, because to be able to pass some kind of context information into an error is a very common thing.

Operating system

Linux

Current behavior

(Protocol.UndefinedError) protocol Enumerable not implemented for type RuntimeError (a struct).

Expected behavior

s.a.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions