ReqLogger (ReqLogger v0.2.0)

Copy Markdown View Source

Logs the request method, URL, response status and duration with Elixir's Logger.

req =
  Req.new()
  |> ReqLogger.attach()

Req.get!(req, url: "https://httpbin.org/status/201?a=1")
# [info] GET https://httpbin.org/status/201 -> 201 (3ms)

Query strings, fragments and userinfo are stripped from logged URLs to avoid logging common places for sensitive values.

When Req retries a request, each attempt is logged separately with its own duration.

Options

:req_logger_level

Either a Logger.level/0:

req =
  Req.new()
  |> ReqLogger.attach(req_logger_level: :debug)

or a function that receives the Req.Response and returns one:

req =
  Req.new()
  |> ReqLogger.attach(req_logger_level: fn
    %Req.Response{status: status} when status >= 500 -> :error
    %Req.Response{} -> :debug
  end)

Defaults to :error for 5xx responses, :warning for 4xx responses and :info for everything else. Failed requests are always logged as :error, regardless of this option.

Invalid values raise an ArgumentError when the plugin is attached, or — for a per-request override — before the request is sent.

Summary

Functions

Attaches the logger to the given request.

Types

opts()

@type opts() :: [
  {:req_logger_level, Logger.level() | (Req.Response.t() -> Logger.level())}
]

Functions

attach(request, opts \\ [])

@spec attach(Req.Request.t(), opts()) :: Req.Request.t()

Attaches the logger to the given request.

Request Options

  • :req_logger_level - the level to log responses at. See the module documentation.

Examples

req = Req.new() |> ReqLogger.attach()
Req.get!(req, url: "https://httpbin.org/status/201?a=1")
# [info] GET https://httpbin.org/status/201 -> 201 (3ms)