Logger Backends JSON 
Yet another (but flexible) JSON backend for Logger. Pick whatever json encoder you want (poison, json, exjsx) or provide your own.
Roadmap
- Basic functionallity, dumb IO usage, configuration and using given parsing lib.
-
Proper formatting of error messages taken from
error_loggerwhich come in as lists - Improve documentation on hex docs.
-
Buffered & async sending messages to IO
:userprocess. - Filtering of messages via configured function in addition to log levels.
- Adding examples of custom json encoders.
- Additional switchable IO backends, i.e. TCP, UDP, File
Installation
The package can be installed as:
-
Add
logger_backends_jsonto your list of dependencies inmix.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
```- 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: PoisonAll possible options:
encoderanything that implementsencode(object) :: map|list => {:ok, json}, we test againstpoison,exjsxandjsonlibs.levelrepresents log level, same as in defaultLogger-debug, info, warn, error.metadataadditional info to pass into json, should beMapin the end.
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
-
This backend doesn't install any dependencies - it doesn't come with any default JSON encoder - so when you try to assign
Poisonas 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
- user process Erl shell workings and what is user process.
- elixir#4720 performance via calling user process directly.
- elixir#4728 performance via buffering IO and sending stuff async.
-
various json loggers (i.e.
json_loggerandlogger_logstash_backend)
License
This source code is released under the MIT License.