Yet another (but flexible) JSON backend for Logger. Pick whatever json encoder you want (poison, json, exjsx) or provide your own.
- Basic functionallity, dumb IO usage, configuration and using given parsing lib.
- Proper formatting of error messages taken from
error_logger
which come in as lists - Improve documentation on hex docs.
- Buffered & async sending messages to IO
:user
process. - Filtering of messages via configured function in addition to log levels.
- Adding examples of custom json encoders.
- Allowing custom message builders.
- Additional switchable IO backends, i.e. TCP, UDP, File
The package can be installed as:
- Add
logger_backends_json
to 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.5.0"}
]
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
```
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")}
alias Logger.Backends.JSON.Event
def create(%Event{} = event), do: %{text: event.message}
def allow?(%Event{} = event), do: event.message != "ping"
end
System.put_env "LOG_LEVEL", "debug"
config :logger, :json,
level: {:system, "LOG_LEVEL", :info},
metadata: {:function, Foo, :bar, [:baz]},
encoder: Poison,
event: Foo
All possible options:
encoder
anything that implementsencode(object) :: map|list => {:ok, json}
, we test againstpoison
,exjsx
andjson
libs.level
represents log level, same as in defaultLogger
-debug, info, warn, error
.metadata
additional info to pass into json, should beMap
in the end.event
a module that implementsbuild(%Event{}) :: map
andallow?(%Event{}) :: boolean
functions.
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.
- 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.
- 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_logger
andlogger_logstash_backend
)
This source code is released under the MIT License.