Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: enhance the Logger middleware document. #504

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/tesla/middleware/logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule Tesla.Middleware.Logger do

## Options

- `:log_level` - custom function for calculating log level (see below)
- `:log_level` - custom option for calculating log level when get `{:ok, response}` after call.(see below)
- `:filter_headers` - sanitizes sensitive headers before logging in debug mode (see below)
- `:debug` - show detailed request/response logging

Expand All @@ -81,7 +81,7 @@ defmodule Tesla.Middleware.Logger do
- `:warn` - for 3xx responses
- `:info` - for 2xx responses

You can customize this setting by providing your own `log_level/1` function:
You can customize this setting by providing your own `log_level/1` function when you get `{:ok, response}` after call:

```
defmodule MyClient do
Expand All @@ -98,6 +98,19 @@ defmodule Tesla.Middleware.Logger do
end
```

In some cases, you can also provide the build-in level directly for customization:

```
defmodule MyClient do
use Tesla
plug Tesla.Middleware.Logger, log_level: :debug
end
```

NOTE:
- **If you get `{:error, whatever}`, the log_level will be `:error`.**
- You can get more log level information from [`Logger.Handler`](https://github.com/elixir-lang/elixir/blob/main/lib/logger/lib/logger/handler.ex)

## Logger Debug output

When the Elixir Logger log level is set to `:debug`
Expand Down
52 changes: 52 additions & 0 deletions test/tesla/middleware/logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ defmodule Tesla.Middleware.LoggerTest do

"/ok" ->
{:ok, %{env | status: 200, body: "ok"}}

"/specific_error" ->
{:error, "a specific error"}
end
end
end
Expand All @@ -185,6 +188,55 @@ defmodule Tesla.Middleware.LoggerTest do
log = capture_log(fn -> ClientWithLogLevel.get("/ok") end)
assert log =~ "[info] GET /ok -> 200"
end

test "specific error" do
log = capture_log(fn -> ClientWithLogLevel.get("/specific_error") end)
assert log =~ "[error] GET /specific_error -> error"
end
end

describe "with log_level which provided build-in level directly" do
defmodule ClientWithLogLevelDirectly do
use Tesla

plug Tesla.Middleware.Logger, log_level: :error

adapter fn env ->
case env.url do
"/bad-request" ->
{:ok, %{env | status: 400, body: "bad request"}}

"/not-found" ->
{:ok, %{env | status: 404, body: "not found"}}

"/ok" ->
{:ok, %{env | status: 200, body: "ok"}}

"/specific_error" ->
{:error, "a specific error"}
end
end
end

test "not found" do
log = capture_log(fn -> ClientWithLogLevelDirectly.get("/not-found") end)
assert log =~ "[error] GET /not-found -> 404"
end

test "bad request" do
log = capture_log(fn -> ClientWithLogLevelDirectly.get("/bad-request") end)
assert log =~ "[error] GET /bad-request -> 400"
end

test "ok" do
log = capture_log(fn -> ClientWithLogLevelDirectly.get("/ok") end)
assert log =~ "[error] GET /ok -> 200"
end

test "specific error" do
log = capture_log(fn -> ClientWithLogLevelDirectly.get("/specific_error") end)
assert log =~ "[error] GET /specific_error -> error"
end
end

describe "with filter_headers" do
Expand Down