pretty_http_logger
A middleware logger library for Dart http
library.
Getting Started
pretty_http_logger
is a module that lets you log request and response for Dart's http
package.
Installing
Include this library in your package.
Importing
Using pretty_http_logger
Create an object of HttpWithMiddleware
by using the build
factory constructor.
The build
constructor takes in a list of middlewares that are built already by pretty_http_logger
.
(You can also build your own middleware for pretty_http_logger
. Check out Build your own middleware)
That is it! Now go ahead and use this http
object as you would normally do.
Request Timeout
With pretty_http_logger
you can also specify the timeout of requests. So if you want a request to be timeout in 30 seconds:
HttpWithMiddleware
supports all the functions that http
provides.
Using a Client
If you want to use a http.Client
in order to keep the connection alive with the server, use HttpClientWithMiddleware
.
Building your own middleware
Building your own middleware with pretty_http_logger
is very easy, whether you want to create a package for pretty_http_logger
or you want to build a middleware solely for your own project.
Once you have the necessary imports, all you have to do is extend the MiddlewareContract
class which will give you access to 3 functions.
interceptRequest(RequestData)
is the method called before any request is made.
interceptResponse(ResponseData)
is the method called after the response from request is received.
interceptError(error)
is the method called when an exception occurs.
You can then @override
all the required functions you need to add middleware to.
Example (A simple logger that logs data in all requests):
You can also modify the RequestData
before the request is made and every ResponseData
after every response is received. For Example, if you want to wrap your data in a particular structure before sending, or you want every request header to have Content-Type
set to application/json
.
Package pretty_http_logger
is inspired by
pretty_http_logger
(https://github.com/mithunadhikari40/pretty_http_logger) : Easy request and response logging.pretty_http_logger
is inspired byhttp_logger
(https://github.com/gurleensethi/http_logger)pretty_http_logger
is inspired byhttp_middleware
(https://github.com/TEDConsulting/http_middleware)pretty_http_logger
is inspired bypretty_dio_logger
(https://github.com/Milad-Akarie/pretty_dio_logger)pretty_http_logger
is a combination of packages mentioned above and uses the code provided by these libraries and keeps everything up to date
If you find any issues or suggestions pretty_http_logger
, open an issue or raise a PR, I will be happy to see.