Created
April 9, 2020 05:37
-
-
Save apgapg/7745133e42a4ed4e114d491a031dc156 to your computer and use it in GitHub Desktop.
Logging interceptor for dio, flutter
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'; | |
import 'package:flutter/cupertino.dart'; | |
/// [LoggingInterceptor] is used to print logs during network requests. | |
/// It's better to add [LoggingInterceptor] to the tail of the interceptor queue, | |
/// otherwise the changes made in the interceptor behind A will not be printed out. | |
/// This is because the execution of interceptors is in the order of addition. | |
class LoggingInterceptor extends Interceptor { | |
LoggingInterceptor(); | |
@override | |
Future onRequest(RequestOptions options) async { | |
logPrint('*** API Request - Start ***'); | |
printKV('URI', options.uri); | |
printKV('METHOD', options.method); | |
logPrint('HEADERS:'); | |
options.headers.forEach((key, v) => printKV(' - $key', v)); | |
logPrint('BODY:'); | |
printAll(options.data ?? ""); | |
logPrint('*** API Request - End ***'); | |
} | |
@override | |
Future onError(DioError err) async { | |
logPrint('*** Api Error - Start ***:'); | |
logPrint('URI: ${err.request.uri}'); | |
if (err.response != null) { | |
logPrint('STATUS CODE: ${err.response.statusCode?.toString()}'); | |
} | |
logPrint('$err'); | |
if (err.response != null) { | |
printKV('REDIRECT', err.response.realUri); | |
logPrint('BODY:'); | |
printAll(err.response?.toString()); | |
} | |
logPrint('*** Api Error - End ***:'); | |
return err; | |
} | |
@override | |
Future onResponse(Response response) async { | |
logPrint('*** Api Response - Start ***'); | |
printKV('URI', response.request?.uri); | |
printKV('STATUS CODE', response.statusCode); | |
printKV('REDIRECT', response.isRedirect); | |
logPrint('BODY:'); | |
printAll(response.data ?? ""); | |
logPrint('*** Api Response - End ***'); | |
return response; | |
} | |
void printKV(String key, Object v) { | |
logPrint('$key: $v'); | |
} | |
void printAll(msg) { | |
msg.toString().split('\n').forEach(logPrint); | |
} | |
void logPrint(String s) { | |
debugPrint(s); | |
} | |
} |
Its showing syntax error?
'LoggingInterceptor.onRequest' ('Future Function(RequestOptions)') isn't a valid override of 'Interceptor.onRequest' ('void Function(RequestOptions, RequestInterceptorHandler)').
Yes, we need to provide an update to this gist as there were breaking changes in dio v4.0.0+
Here is the code adapted to dio v4. I haven't tested everything, though.
Thanks for the gist 👏
import 'dart:async';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
/// [LoggingInterceptor] is used to print logs during network requests.
/// It's better to add [LoggingInterceptor] to the tail of the interceptor queue,
/// otherwise the changes made in the interceptor behind A will not be printed out.
/// This is because the execution of interceptors is in the order of addition.
class LoggingInterceptor extends Interceptor {
LoggingInterceptor();
@override
Future onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
logPrint('*** API Request - Start ***');
printKV('URI', options.uri);
printKV('METHOD', options.method);
logPrint('HEADERS:');
options.headers.forEach((key, v) => printKV(' - $key', v));
logPrint('BODY:');
printAll(options.data ?? '');
logPrint('*** API Request - End ***');
return handler.next(options);
}
@override
void onError(DioError err, ErrorInterceptorHandler handler) {
logPrint('*** Api Error - Start ***:');
logPrint('URI: ${err.requestOptions.uri}');
if (err.response != null) {
logPrint('STATUS CODE: ${err.response?.statusCode?.toString()}');
}
logPrint('$err');
if (err.response != null) {
printKV('REDIRECT', err.response?.realUri ?? '');
logPrint('BODY:');
printAll(err.response?.data.toString());
}
logPrint('*** Api Error - End ***:');
return handler.next(err);
}
@override
Future onResponse(Response response, ResponseInterceptorHandler handler) async {
logPrint('*** Api Response - Start ***');
printKV('URI', response.requestOptions.uri);
printKV('STATUS CODE', response.statusCode ?? '');
printKV('REDIRECT', response.isRedirect ?? false);
logPrint('BODY:');
printAll(response.data ?? '');
logPrint('*** Api Response - End ***');
return handler.next(response);
}
void printKV(String key, Object v) {
logPrint('$key: $v');
}
void printAll(msg) {
msg.toString().split('\n').forEach(logPrint);
}
void logPrint(String s) {
debugPrint(s);
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample response on console