LogflareEx.LoggerBackend (logflare_ex v0.2.0-dev.980340db)

Implements :gen_event behaviour, handles incoming Logger messages.

Usage

Add the following to your config.exs:

config :logger
  backends: [..., LogflareEx.LoggerBackend]

You can then log as per normal:

require Logger
Logger.info("some event", my: "data")

Metadata

To add custom metadata, use Logger.metadata/1

Logger.metadata(some: "data")

On the payload sent to Logflare API, the above metadata will be merged into the metadata field, resulting in the following schema path metadata.some.

Any additional Logger metadata provided in the Logger.info/2 call will be merged together by Logger. This merging is not handled by the library.

This backend also enriches the log event with certaon fields:

  • metadata.context - context of the log event, including vm information, pid, module, etc.
  • metadata.level - log level, corresponding to the Logger level set.
  • metadata.stacktrace - stacktrace of the error, if the event is an error.

JSON-Encoding Conversions

To ensure that payload sent to Logflare API is JSON serializable and searchable by the selected backend, certain conversions are applied to the terms received.

  • atoms are converted to strings. For example, :value to "value".
  • charlists are converted to strings For example, 'value' to "value".
  • tuples converted to lists For example, {1, 2} to [1, 2].
  • keyword lists converted to maps For example, [my: :value] to %{"my"=> "value"}.
  • structs converted to maps For example, %MyStruct{} to %{some: "default key"}.
  • NaiveDateTime and DateTime are converted using the String.Chars protocol For example, %NaiveDateTime{} to 1337-04-19 00:00:00.
  • pids are converted to strings For example, #PID<0.109.0> to "<0.109.0>"

Summary

Types

@type level() :: Logger.level()
@type log_msg() :: {level(), pid(), {Logger, message(), term(), metadata()}} | :flush
@type message() :: Logger.message()
@type metadata() :: Logger.metadata()

Functions

Link to this function

code_change(old_vsn, config, extra)

Callback implementation for :gen_event.code_change/3.

Link to this function

handle_call(arg, config)

Callback implementation for :gen_event.handle_call/2.

Link to this function

handle_event(arg1, config)

@spec handle_event(log_msg(), Config.t()) :: {:ok, Config.t()}

Callback implementation for :gen_event.handle_event/2.

Link to this function

handle_info(arg1, state)

Callback implementation for :gen_event.handle_info/2.

Link to this function

init(_MODULE__)

@spec init(LogflareEx.LoggerBackend) :: {:ok, map()}

Callback implementation for :gen_event.init/1.

Link to this function

terminate(reason, state)

Callback implementation for :gen_event.terminate/2.