Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c7c1da
commit 94a8b1b
Showing
4 changed files
with
84 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
defmodule Maxwell.Middleware.Logger do | ||
@moduledoc """ | ||
Log the request | ||
Log the response | ||
```ex | ||
# Client.ex | ||
use Maxwell.Builder ~(get)a | ||
@middleware Maxwell.Middleware.Log | ||
def request do | ||
"/test" |> url |> get! | ||
end | ||
``` | ||
""" | ||
use Maxwell.Middleware | ||
require Logger | ||
|
||
def request(env, opts) do | ||
"REQUEST: " | ||
|> Kernel.<>(format_message(env)) | ||
|> Logger.info | ||
env | ||
end | ||
def response(result, log_fun) do | ||
case result do | ||
{:error, reason} -> | ||
format_reason = :io_lib.format("~p", [reason]) | ||
Logger.error("RESPONSE ERROR: " <> format_reason) | ||
{:ok, env} -> | ||
message = "RESPONSE: " <> format_message(env) | ||
cond do | ||
env.status >= 400 -> Logger.error message | ||
env.status >= 300 -> Logger.warn message | ||
true -> Logger.info message | ||
end | ||
end | ||
result | ||
end | ||
|
||
defp format_message(env) do | ||
method = env.method |> to_string |> String.upcase | ||
status = case env.status do | ||
nil -> ""; | ||
200 -> "#{IO.ANSI.green} => 200" | ||
status1 -> " => #{status1}" | ||
end | ||
header = case Map.equal?(env.headers, %{}) do | ||
true -> "" | ||
false -> :io_lib.format("~p", [env.headers]) | ||
end | ||
options = case env.opts do | ||
[] -> "" | ||
options -> :io_lib.format("~p", [options]) | ||
end | ||
"#{method} #{env.url} #{header} #{options} #{status}" | ||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule LoggerTest do | ||
use ExUnit.Case | ||
import Maxwell.TestHelper | ||
|
||
test "Middleware.Logger request" do | ||
env = request(Maxwell.Middleware.Logger, %Maxwell{url: "/path"}, []) | ||
assert env == %Maxwell{url: "/path"} | ||
end | ||
|
||
test "Middleware.Logger response" do | ||
{:ok, env} = response(Maxwell.Middleware.Logger, {:ok, %Maxwell{url: "/path"}}, []) | ||
assert env == %Maxwell{url: "/path"} | ||
end | ||
|
||
end | ||
|