Created
April 26, 2020 10:47
-
-
Save IhwanID/ea8eae691d66aa66bfc4eecb1c4bf9cf to your computer and use it in GitHub Desktop.
Logging Interceptor with Dio
This file contains hidden or 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
| import 'dart:async'; | |
| import 'package:dio/dio.dart'; | |
| class LoggingInterceptors extends Interceptor { | |
| @override | |
| Future<FutureOr> onRequest(RequestOptions options) async { | |
| print( | |
| "--> ${options.method != null ? options.method.toUpperCase() : 'METHOD'} ${"" + (options.baseUrl ?? "") + (options.path ?? "")}"); | |
| print("Headers:"); | |
| options.headers.forEach((k, v) => print('$k: $v')); | |
| if (options.queryParameters != null) { | |
| print("queryParameters:"); | |
| options.queryParameters.forEach((k, v) => print('$k: $v')); | |
| } | |
| if (options.data != null) { | |
| print("Body: ${options.data}"); | |
| } | |
| print( | |
| "--> END ${options.method != null ? options.method.toUpperCase() : 'METHOD'}"); | |
| return options; | |
| } | |
| @override | |
| Future<FutureOr> onError(DioError dioError) async { | |
| print( | |
| "<-- ${dioError.message} ${(dioError.response?.request != null ? (dioError.response.request.baseUrl + dioError.response.request.path) : 'URL')}"); | |
| print( | |
| "${dioError.response != null ? dioError.response.data : 'Unknown Error'}"); | |
| print("<-- End error"); | |
| } | |
| @override | |
| Future<FutureOr> onResponse(Response response) async { | |
| print( | |
| "<-- ${response.statusCode} ${(response.request != null ? (response.request.baseUrl + response.request.path) : 'URL')}"); | |
| print("Headers:"); | |
| response.headers?.forEach((k, v) => print('$k: $v')); | |
| print("Response: ${response.data}"); | |
| print("<-- END HTTP"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment