dio_logger_plus 1.0.0
dio_logger_plus: ^1.0.0 copied to clipboard
Custom logging interceptor for the Dio
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": "test@example.com",
"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.