dio_logger_plus 1.1.0
dio_logger_plus: ^1.1.0 copied to clipboard
A powerful and customizable Dio interceptor for Flutter that logs HTTP requests, responses, headers, and errors with pretty-printed JSON. Useful for debugging and development.
DioLoggerPlus #
DioLoggerPlus is a custom logging interceptor for the Dio HTTP client in Flutter. It helps developers easily debug HTTP requests and responses by printing clean, formatted logs in the console.
โจ Features #
- โ Logs HTTP requests and responses
- ๐ฆ Pretty-prints request/response bodies
- ๐ธ Logs request headers
- โ Logs errors with status and response body
- ๐งฉ Optional compact or indented JSON
- ๐ Fully customizable
- ๐ Supports debug-only logging (enabled via
kDebugMode)
๐ฆ Installation #
Add dio to your pubspec.yaml:
dependencies:
dio: ^5.0.0
Then copy the DioLoggerPlus class into your project.
๐ Usage #
Add the interceptor to your Dio instance:
import 'package:dio/dio.dart';
import 'dio_logger_plus.dart'; // import your class
final dio = Dio();
dio.interceptors.add(DioLoggerPlus(
request: true,
requestHeader: true,
requestBody: true,
responseBody: true,
error: true,
compact: true,
maxWidth: 90,
isOnlyDebug: true,
));
โ๏ธ Configuration Options #
| Parameter | Type | Default | Description |
|---|---|---|---|
request |
bool |
true |
Logs HTTP method and URL |
requestHeader |
bool |
true |
Logs request headers |
requestBody |
bool |
true |
Logs request body |
responseBody |
bool |
true |
Logs response body |
error |
bool |
true |
Logs errors and error bodies |
compact |
bool |
true |
Minimized JSON indentation |
maxWidth |
int |
90 |
(Reserved) Max width of the log lines |
isOnlyDebug |
bool |
true |
Enables logging only in debug mode |
๐งช Example Output #
โก๏ธ REQUEST โ POST https://api.example.com/login
๐ธ Headers: {"Content-Type":"application/json"}
๐ฆ Body:
{
"email": "[email protected]",
"password": "123456"
}
โ
RESPONSE:https://api.example.com/login:
{
"token": "abc123xyz"
}
โ ERROR โ https://api.example.com/login
โ๏ธ Message: Unauthorized
๐ Error Body:
{
"error": "Invalid credentials"
}
๐ Debug-Only Logging
By default, logs are shown only in debug mode (kDebugMode). To enable logs in release mode, set isOnlyDebug: false.