Timber v3.1.2 Timber.Contextable protocol View Source

Converts a data structure into a Timber.Context.t. This is called on any data structure passed in the Timber.add_context/1 function.

For example, this protocol is how we're able to support Keyword.t types:

Timber.add_context(build: %{version: "1.0"})

This is achieved by:

defimpl Timber.Contextable, for: Map do
  def to_context(map) when map_size(map) == 1 do
    [type] = Map.keys(map)
    [data] = Map.values(map)
    %Timber.Contexts.CustomContext{
      type: type,
      data: data
    }
  end
end

What about custom contexts and structs?

If you decide to get more formal with you event definition strategy you can use this like you would any other protocol:

def OrderPlacedEvent do
  defstruct [:id, :total]

  defimpl Timber.Contextable do
    def to_context(event) do
      Map.from_struct(event)
    end
  end
end

Link to this section Summary

Functions

Converts the data structure into a Timber.Context.t

Link to this section Types

Link to this section Functions

Converts the data structure into a Timber.Context.t.