Charlist values are not showing up in Logger metadata

Is there a reason Elixir doesn’t support charlists as values in the Logger metadata? Here’s a list of things it supports today:

I noticed this while adding opentelemetry-erlang integration to the project. opentelemetry-erlang uses values of type charlist to add span context info to log metadata, and they weren’t showing up in the logs in place where the metadata should be printed:

There are some ways to work around the problem of charlist not showing up in the logs, but I got curious if there’s a reason Elixir couldn’t support charlists :thinking:

I guess the reason is because Elixir uses binaries and charlists are only for interoperability with Erlang. You’re integrating with an Erlang library which expects charlists. Elixir got you covered there. A similar question could be asked to the developers of the library: why aren’t they accepting binaries.

  * Implement the `String.Chars` protocol

Technically this should also include charlists, so perhaps they shouldn’t be discarded order to fit this description?

This is a bit of a tricky problem given elixir uses :logger nowadays. Idealy one wouldn’t need to care if the host system is an elixir or erlang system to use a logger handler and with some configuration effort you can get there, but it’s not (yet) possible with elixir ootb.

2 Likes

Yes, I noticed this too and had a similar question.

I thought charlists are skipped because validating their contents is not easy / performant enough for such a critical area as logging: e.g. that lists do not contain values other than characters, or that all characters are printable, etc.

But maybe I am wrong an Elixir could indeed implement support for charlists in metadata?

1 Like

@josevalim would you have an opinion on this? Can we make charlists renderable in Logger formatter?

I think the main problem there is checking whether given list is charlist or not, as that may require deep traversal. Also, if we support chartists, then should we also support IO lists? What about charlists with non-ASCII characters in them? Unfortunately, this is place where static typing would be useful, but without that, it may be computationally expensive to check given list on each log message.

4 Likes

The current behavior is now officially documented (i.e. excluding charlists): Clarify that charlists cannot be used as logger metadata by sabiwara · Pull Request #13977 · elixir-lang/elixir · GitHub

Jose’s answer:

IIRC we ran into issue once we integrated with Erlang’s logger, so for now I would only document the current behaviour.

2 Likes

Thanks, @sabiwara!