Zexbox.Logging.JsonFormatter (Zexbox v1.8.0)

Copy Markdown View Source

Builds a :logger formatter that writes every log event as one line of JSON, using LoggerJSON.Formatters.Basic with Zappi defaults.

Filebeat ships each line as its own Elasticsearch document, so plain-text output with struct inspections, multi-line SQL or stack traces fans out into many documents. One JSON line per event keeps it to one.

Requires {:logger_json, "~> 7.0"} in the app's deps. Call it from config/runtime.exs:

config :logger, :default_handler,
  formatter: Zexbox.Logging.JsonFormatter.config()

It can't be called from config/config.exs or config/prod.exs: those are evaluated before dependencies are compiled.

Summary

Functions

Returns the {formatter_module, config} tuple for config :logger, :default_handler, formatter: ....

The default metadata allow-list.

The default redactors.

Functions

config(opts \\ [])

@spec config(keyword()) :: {module(), map()}

Returns the {formatter_module, config} tuple for config :logger, :default_handler, formatter: ....

Takes any option LoggerJSON.Formatters.Basic.new/1 accepts. Options given replace the defaults for that key:

  • :metadata - defaults to default_metadata/0. Avoid :all in production. Trace and span IDs are always written as top-level trace and span fields.
  • :redactors - defaults to default_redactors/0. Pass [] to disable. Redactors match exact key names and only apply to metadata and map or keyword messages, never to string messages.
  • :encoder_opts - passed straight through.

Examples

iex> {module, _config} = Zexbox.Logging.JsonFormatter.config()
iex> module
LoggerJSON.Formatters.Basic

default_metadata()

@spec default_metadata() :: [atom()]

The default metadata allow-list.

default_redactors()

@spec default_redactors() :: [{module(), term()}]

The default redactors.