View Source MetadataLogger (metadata_logger v0.3.2)

Logging with metadata.

configuration

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

Link to this function

format(level, message, ts, metadata)

View Source

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

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"
}

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"
}
Link to this function

log_to_map(level, message, ts, metadata)

View Source
@spec log_to_map(
  Logger.level(),
  Logger.message() | Logger.report(),
  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
  • :gl
  • :crash_reason
  • :initial_call
  • :registered_name
  • :domain
  • :ancestors
  • :callers

Followings metadata will be removed:

  • :mfa: see :module and :funtion
  • :report_cb

examples

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]
}

iex> MetadataLogger.log_to_map(
...>   :info,
...>   %{hello: :world},
...>   {{2019, 11, 22}, {12, 23, 45, 678}},
...>   []
...> )
%{
  level: :info,
  message: %{hello: :world},
  metadata: %{},
  timestamp: ~N[2019-11-22 12:23:45.000678]
}

iex> MetadataLogger.log_to_map(
...>   :info,
...>   [foo: 1, foo: 2],
...>   {{2019, 11, 22}, {12, 23, 45, 678}},
...>   []
...> )
%{
  level: :info,
  message: %{foo: 2},
  metadata: %{},
  timestamp: ~N[2019-11-22 12:23:45.000678]
}