phoenix_json_logger v0.1.0 PhoenixJsonLogger

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 PhoenixJsonLogger, 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 PhoenixJsonLogger, 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 Jason 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()

opts() ::
  binary()
  | tuple()
  | atom()
  | integer()
  | float()
  | [opts()]
  | %{required(opts()) => opts()}

Type for a plug option

Type for time

Link to this section Functions

Link to this function

call(conn, level_or_opts)

call(Plug.Conn.t(), opts()) :: Plug.Conn.t()
Link to this function

init(opts)

init(opts()) :: opts()
Link to this function

log(conn, level, start, opts \\ [])

log(Plug.Conn.t(), atom(), time(), opts()) :: atom() | no_return()
Link to this function

log_error(kind, reason, stacktrace)

log_error(atom(), map(), list()) :: atom()