Plug Logger JSON v0.5.0 Plug.LoggerJSON View Source

A plug for logging basic request information in the format:

{
  "api_version":     "N/A"
  "client_ip":       "23.235.46.37"
  "client_version":  "ios/1.6.7",
  "date_time":       "2016-05-31T18:00:13Z",
  "duration":        4.670,
  "handler":         "fronts#index"
  "log_type":        "http",
  "method":          "POST",
  "params":          {
                       "user":"jkelly",
                       "password":"[FILTERED]"
                     },
  "path":            "/",
  "request_id":      "d90jcl66vp09r8tke3utjsd1pjrg4ln8",
  "status":          "200"
}

To use it, just plug it into the desired module. plug Plug.LoggerJSON, log: :debug

Options

  • :log - The log level at which this plug should log its request info. Default is :info.
  • :extra_attributes_fn - Function to call with conn to add additional fields to the requests. Default is nil. Please see “Extra Fields” section for more information.

Extra Fields

Additional data can be logged alongside the request by specifying a function to call which returns a map:

  def extra_attributes(conn) do
    map = %{
      "user_id" => get_in(conn.assigns, [:user, :user_id]),
      "other_id" => get_in(conn.private, [:private_resource, :id]),
      "should_not_appear" => conn.private[:does_not_exist]
    }

    map
    |> Enum.filter(&(&1 !== nil))
    |> Enum.into(%{})
  end

  plug Plug.LoggerJSON, log: Logger.level,
                        extra_attributes_fn: &MyPlug.extra_attributes/1

In this example, the :user_id is retrieved from conn.assigns.user.user_id and added to the log if it exists. In the example, any values that are nil are filtered from the map. It is a requirement that the value is serialiazable as JSON by the Poison library, otherwise an error will be raised when attempting to encode the value.

Link to this section Summary

Types

Type for a plug option

Type for time

Link to this section Types

Link to this type opts() View Source
opts ::
  binary |
  tuple |
  atom |
  integer |
  float |
  [opts] |
  %{optional(opts) => opts}

Type for a plug option

Link to this type time() View Source
time() :: {non_neg_integer, non_neg_integer, non_neg_integer}

Type for time

Link to this section Functions

Callback implementation for Plug.call/2.

Callback implementation for Plug.init/1.

Link to this function log(conn, level, start, opts \\ []) View Source
log(Plug.Conn.t, atom, time, opts) :: atom | no_return
Link to this function log_error(kind, reason, stacktrace) View Source
log_error(atom, map, list) :: atom