elixir-lang / elixir

Elixir is a dynamic, functional language for building scalable and maintainable applications
https://elixir-lang.org/
Apache License 2.0
24.38k stars 3.36k forks source link

Logger metadata doesn't support Ports as values #12328

Closed bbhoss closed 1 year ago

bbhoss commented 1 year ago

Elixir and Erlang/OTP versions

Erlang/OTP 25 [erts-13.1.3] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Elixir 1.14.2 (compiled with Erlang/OTP 25)

Operating system

macOS 13.1

Current behavior

I am working with :gen_tcp and sockets. I wanted to add the client socket do my Logger metadata to assist in an investigation. So I tried this

Logger.metadata(client_socket: csock) # csock is a Port

I have my Logger configured like this

config :logger, :console,
 format: "[$level] $message $metadata\n",
 metadata: [:pid, :client_socket]

when I log like this:

Logger.debug("RX Request: #{inspect(client_bin)}")

The output I receive is

[debug] RX Request: "I" pid=<0.244.0>
[debug] RX Request: <<0, 0, 48>> pid=<0.244.0>
[debug] RX Request: <<57, 0, 0, 0, 101>> pid=<0.244.0>
[debug] RX Request: <<73, 0, 0>> pid=<0.244.0>

Converting the port to a string for the metadata works fine:

Logger.metadata(client_socket: inspect(csock))
[debug] RX Request: <<0, 0, 48, 57>> pid=<0.227.0> client_socket=#Port<0.8>
[debug] RX Request: <<0, 0, 0, 101>> pid=<0.227.0> client_socket=#Port<0.8>
[debug] RX Request: <<73, 0, 0, 48>> pid=<0.227.0> client_socket=#Port<0.8>
[debug] RX Request: <<58, 0, 0, 0>> pid=<0.227.0> client_socket=#Port<0.8>

Expected behavior

I should see the port converted to string like this:

[debug] RX Request: <<0, 0, 48, 57>> pid=<0.227.0> client_socket=#Port<0.8>
[debug] RX Request: <<0, 0, 0, 101>> pid=<0.227.0> client_socket=#Port<0.8>
[debug] RX Request: <<73, 0, 0, 48>> pid=<0.227.0> client_socket=#Port<0.8>
[debug] RX Request: <<58, 0, 0, 0>> pid=<0.227.0> client_socket=#Port<0.8>

It seems like the rest of the gang is covered, pids, refs, etc get converted to a string automatically, but not ports. Currently this is a silent failure. I think it's probably a one line fix that I'm happy to take on unless I'm missing something.

josevalim commented 1 year ago

A PR is welcome!