Logger functions should accept more than just maps or list as metadata
Closed this issue · 2 comments
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.
I am against this proposal because there is no guarantee that the fields of a struct are useful metadata. It could even lead to security issues, such as someone passing a whole user struct as metadata and now you have fields like tokens and passwords being sent to your logger service, or lead to privacy violations. Having a clear API makes sure you’re sending the correct data.
Also, in other languages, the logging mechanism often accepts multiple arguments that are converted to string, and that that’s not what is happening here. Trying to mimic their API With a different behavior is going to lead to more confusion.
If you want to have more discussion on this feature, I recommend sending to the core mailing list, which is the recommended path for feature discussions. Thank you.
Ok thanks! I'll have a look at the docs if somehting can be clarified there.