loggable

Protocol for custom views of data in logs (hide sensitive data etc)


License
MIT

Documentation

Loggable

Sometimes it's needed to hide some chunks of data before applying Logger functions. For example credit card numbers, passwords and other sensitive information. Loggable protocol helps us to do it for any type of data. It has fallback to Any (does not do anything by default).

Usage

defmodule User do
  @enforce_keys [:name, :balance, :card]
  defstruct @enforce_keys
end

defimpl Loggable.Protocol, for: User do
  def show(%User{} = x) do
    %User{x | card: "[SECRET]"}
  end
end

and then

iex> x = %User{
...>   name: "Jessy",
...>   balance: 100,
...>   card: "4510 6459 8301 6543"
...> }
%User{balance: 100, card: "4510 6459 8301 6543", name: "Jessy"}

iex> Loggable.show(x)
%User{balance: 100, card: "[SECRET]", name: "Jessy"}

iex> Loggable.show(%{foo: [x, x, x]})
%{
  foo: [
    %User{balance: 100, card: "[SECRET]", name: "Jessy"},
    %User{balance: 100, card: "[SECRET]", name: "Jessy"},
    %User{balance: 100, card: "[SECRET]", name: "Jessy"}
  ]
}

Installation

The package can be installed by adding loggable to your list of dependencies in mix.exs:

def deps do
  [
    {:loggable, "~> 0.1.0"}
  ]
end