View Source LoggerJSON.Plug (logger_json v6.0.0-rc.0)

A Plug to log request information in JSON format.

It works by setting up a Plug.Conn.register_before_send/2 callback which logs a structured message with conn and latency keys, that are handled by the formatter when the message is encoded.

This module is not recommended to be used in production, as it can be costly to log every single database query.

Summary

Functions

Attaches the telemetry handler to the given event.

A telemetry handler that logs requests in a structured format.

Functions

Link to this function

attach(name, event, level)

View Source

Attaches the telemetry handler to the given event.

Available options

  • :level - log level which is used to log requests. Defaults to :info.

Dynamic log level

In some cases you may wish to set the log level dynamically on a per-query basis. To do so, set the :level option to a tuple, {Mod, Fun, Args}. The query and map of time measures will be prepended to the provided list of arguments.

When invoked, your function must return a Logger.level() or false to disable logging for the request.

Examples

Attaching the telemetry handler to the MyApp.Repo events with the :info log level:

# in the endpoint
plug Plug.Telemetry, event_prefix: [:myapp, :plug]

# in your application.ex
LoggerJSON.Plug.attach("logger-json-requests, [:myapp, :plug, :stop], :info)

To make plug broadcast those events see Plug.Telemetry documentation.

You can also attach to the [:phoenix, :endpoint, :stop] event to log request latency from Phoenix endpoints:

LoggerJSON.Plug.attach("logger-json-phoenix-requests, [:phoenix, :endpoint, :stop], :info)
Link to this function

telemetry_logging_handler(event_name, map1, map2, level)

View Source
@spec telemetry_logging_handler(
  event_name :: [atom()],
  query_time :: %{duration: non_neg_integer()},
  metadata :: %{conn: Plug.Conn.t()},
  level ::
    Logger.level()
    | {module :: module(), function :: atom(), arguments :: [term()]}
    | false
) :: :ok

A telemetry handler that logs requests in a structured format.