metadata_logger v0.2.1 MetadataLogger View Source
Logging with metadata.
Configuration
config :logger, :console,
level: :debug,
format: {MetadataLogger, :format},
colors: [enabled: false],
metadata: :all,
truncate: :infinity,
utc_log: true
Link to this section Summary
Functions
Formatter function to print message and metadata in a single-line json.
Get a map from log formatter arguments.
Link to this section Functions
format(level, message, ts, metadata)
View Sourceformat(Logger.level(), Logger.message(), Logger.Formatter.time(), keyword()) :: IO.chardata()
Formatter function to print message and metadata in a single-line json.
Make sure all metadata except known metadata encodable by Jason.encode_to_iodata!/2
.
See log_to_map/4
for transformation on known metadata.
It removes :function
, :file
, and :line
from the log.
Examples
iex> MetadataLogger.format(
...> :info,
...> ["hello", " ", "world"],
...> {{2019, 11, 22}, {12, 23, 45, 678}},
...> function: "hello/1",
...> file: "/my/file.ex",
...> line: 11,
...> foo: :bar
...> ) |> Jason.decode!()
%{
"level" => "info",
"message" => "hello world",
"metadata" => %{"foo" => "bar"},
"timestamp" => "2019-11-22T12:23:45.000678"
}
log_to_map(level, message, ts, metadata)
View Sourcelog_to_map(Logger.level(), Logger.message(), Logger.Formatter.time(), keyword()) :: map()
Get a map from log formatter arguments.
It converts Logger.Formatter.time/0
into NaiveDateTime
so it is recommended to configure logger to use UTC by setting :utc_log
to true
.
It converts metadata keyword into map using Enum.into/2
therefore duplicated keys will be removed.
It moves following known metadata to the top level. See Logger Metadata for details.
:application
:module
:function
:file
:line
:pid
:crash_reason
:initial_call
:registered_name
Examples
iex> MetadataLogger.log_to_map(
...> :info,
...> ["hello", " ", "world"],
...> {{2019, 11, 22}, {12, 23, 45, 678}},
...> foo: :bar
...> )
%{
level: :info,
message: "hello world",
metadata: %{foo: :bar},
timestamp: ~N[2019-11-22 12:23:45.000678]
}