Logger Backends JSON Build Status

Yet another (but flexible) JSON backend for Logger. Pick whatever json encoder you want (poison, json, exjsx) or provide your own.

Roadmap

Installation

The package can be installed as:

  1. Add logger_backends_json to your list of dependencies in mix.exs:
```elixir
def deps do
  [
    # your json library of choice, otherwise you will need to provide custom module.
    {:poison, "~> 3.0"},
    {:logger_backends_json, "~> 0.3.1"}
  ]
end
```
  1. Update your config
```elixir
# A tuple with module and config name to use
config :logger, backends: [{Logger.Backends.JSON, :json}]

config :logger, :json,
  level: :info,
  metadata: %{foo: "bar"},
  encoder: Poison
```

Extra configuration

This library uses ConfigExt for loading dynamic configuration, like environment variables or functions on runtime.

defmodule Foo do
  def bar(baz), do: %{bar: inspect(baz), env: System.get_env("APP_ENV")}
end

System.put_env "LOG_LEVEL", "debug"

config :logger, :json,
  level: {:system, "LOG_LEVEL", :info},
  metadata: {:function, Foo, :bar, [:baz]},
  encoder: Poison

All possible options:

In any case you can also specify a function that will get evaluated on initialization, if you need to update it during runtime - just run Logger.configure_backend(...).

If you need to pass any extra info on each log, i.e. some stuff from ETS tables or whatever, you can do it by creating custom encoder and adding it there.

Frequent issues

  1. This backend doesn't install any dependencies - it doesn't come with any default JSON encoder - so when you try to assign Poison as encoder, but you didn't installed it you will get a message like
```elixir
iex(1)>
=INFO REPORT==== 16-Nov-2016::09:57:32 ===
    application: logger
    exited: shutdown
    type: temporary
```

to solve it just install `:poison` (or any other json lib) by following their installation instructions, and everything should be back to good.

Sources & inspiration

License

This source code is released under the MIT License.