How do you log a map/struct type in ELixir without having to implement protocol String.Chars
?
require Logger
Logger.debug %{my: "map"}
** (Protocol.UndefinedError) protocol String.Chars not implemented for %{my: "map"}
(elixir) lib/string/chars.ex:3: String.Chars.impl_for!/1
(elixir) lib/string/chars.ex:17: String.Chars.to_string/1
In Elixir, structs can only ever have the keys that you assigned to it in the defstruct macro. You can use %{struct | key => value} just fine as long as the value of key is one of the keys that struct has.
To define a struct, the defstruct construct is used: iex> defmodule User do ...> defstruct name: "John", age: 27 ...> end. The keyword list used with defstruct defines what fields the struct will have along with their default values. Structs take the name of the module they're defined in.
You can use inspect/2
- https://hexdocs.pm/elixir/Kernel.html#inspect/2
It parses the data structure into an algebra document which can be printed with the logger.
iex(4)> Logger.debug inspect(%{a: 1})
08:47:32.776 [debug] %{a: 1}
:ok
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With