-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from inaka/flavio.33.inspect-request-response
[#33] inspect request response, better logging
- Loading branch information
Showing
14 changed files
with
414 additions
and
162 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
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 |
---|---|---|
@@ -1,47 +1,28 @@ | ||
defmodule Dayron.Logger do | ||
@moduledoc """ | ||
Helper module wrapping Logger calls to register request/response events | ||
""" | ||
require Logger | ||
alias Dayron.Response | ||
alias Dayron.ClientError | ||
@moduledoc ~S""" | ||
Behaviour for creating Dayron Loggers | ||
@doc """ | ||
Logs a debug or error message based on response code. | ||
""" | ||
def log(method, url, response, req_details \\ []) do | ||
do_log(method, url, response, req_details) | ||
response | ||
end | ||
Loggers are responsible to print request and response data to an output. | ||
@doc """ | ||
Logs a debug message for response codes between 200-399. | ||
""" | ||
def do_log(method, url, %Response{status_code: code}, req_details) when code < 400 do | ||
Logger.debug [method, ?\s, url, ?\s, "-> #{code}"] | ||
log_request_details :debug, req_details | ||
end | ||
## Example | ||
@doc """ | ||
Logs an error message for error response codes, or greater than 400. | ||
defmodule Dayron.CustomLogger do | ||
@behaviour Dayron.Logger | ||
require Logger | ||
def log(request, response) do | ||
Logger.debug(inspect(request)) | ||
Logger.debug(inspect(response)) | ||
end | ||
end | ||
""" | ||
def do_log(method, url, %Response{status_code: code}, req_details) do | ||
Logger.error [method, ?\s, url, ?\s, "-> #{code}"] | ||
log_request_details :debug, req_details | ||
end | ||
alias Dayron.Request | ||
alias Dayron.Response | ||
alias Dayron.ClientError | ||
|
||
@doc """ | ||
Logs an error message for response error/exception. | ||
Logs an message based on request and response data. | ||
""" | ||
def do_log(method, url, %ClientError{reason: reason}, req_details) do | ||
Logger.error [method, ?\s, url, ?\s, "-> #{reason}"] | ||
log_request_details :error, req_details | ||
end | ||
|
||
defp log_request_details(level, req_details) do | ||
if Enum.any?(req_details) do | ||
Logger.log(level, "Request: \n #{inspect req_details, pretty: true}") | ||
end | ||
end | ||
|
||
@callback log(Request.t, Response.t | ClientError.t) :: atom | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule Dayron.BasicLogger do | ||
@moduledoc """ | ||
Helper module wrapping Logger calls to register request/response events | ||
""" | ||
require Logger | ||
alias Dayron.Response | ||
alias Dayron.ClientError | ||
|
||
@doc """ | ||
Logs a debug message for response codes between 200-399. | ||
""" | ||
def log(request, %Response{status_code: code} = response) when code < 400 do | ||
do_log(:debug, request, response) | ||
end | ||
|
||
@doc """ | ||
Logs an error message for error response codes, or greater than 400. | ||
""" | ||
def log(request, %Response{} = response), do: | ||
do_log(:error, request, response) | ||
|
||
@doc """ | ||
Logs an error message for response error/exception. | ||
""" | ||
def log(request, %ClientError{} = response) do | ||
response = %{response | request: request} | ||
Logger.error ClientError.message(response) | ||
end | ||
|
||
defp do_log(level, request, response) do | ||
Logger.log level, inspect(request, pretty: true) | ||
Logger.log level, inspect(response, pretty: true) | ||
end | ||
end |
Oops, something went wrong.