A custom formatter for Erlang OTP logger that outputs JSON using jsx
This project should be added as a dependency to your project
sys.config
:
[ { kernel
, [ { logger
,[ { handler
, default
, logger_std_h
, #{formatter => {jsonformat, #{new_line => true}}}
}
] }
, {logger_level, info}
] }
].
shell
:
> logger:info(#{a => b}).
{"a":"b","gl":"<0.178.0>","level":"info","pid":"<0.182.0>","report_cb":"fun logger:format_otp_report/1","time":1585902924341139}ok
To print each json object to a new line, set new_line
to true
:
#{formatter => {jsonformat, #{ new_line => true }}}
To control what is being included in the log object from the metadata, there
are two ways. One can opt-out from fields. Default opts out is [report_cb]
.
#{ meta_without => [report_cb, gl, file, domain] }
Or for very detailed control there is instead opt-in.
#{ meta_with => [time, mfa, line, user_key, client_key] }
To rename keys in the resulting json object, provide a key_mapping
. For
example, to rename the time
and level
keys to timestamp
and lvl
respectively, use:
#{formatter => {jsonformat, #{ key_mapping => #{ time => timestamp
, level => lvl }}}}
To format values in the resulting json object, provide a map of format_funs
.
For example, to format the value associated with the time
key, use:
#{formatter => {jsonformat, #{ format_funs => #{ time => fun(T) -> ... end }}}
Note that key_mapping
s are applied before format_funs
.
fun jsonformat:system_time_to_iso8601/1
Will take a logger:timestamp() and print it in the format of yyyy-mm-ddTHH:MM:SSZ
$ rebar3 eunit
We use SemVer for versioning. For the versions available, see the tags on this repository.
We use Conventional Commits.
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
This project is licensed under the MIT License - see the LICENSE file for details