Skip to content

Instantly share code, notes, and snippets.

@apgapg
Created April 9, 2020 05:37
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save apgapg/7745133e42a4ed4e114d491a031dc156 to your computer and use it in GitHub Desktop.
Save apgapg/7745133e42a4ed4e114d491a031dc156 to your computer and use it in GitHub Desktop.
Logging interceptor for dio, flutter
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);
}
}
@apgapg
Copy link
Author

apgapg commented Apr 9, 2020

Sample response on console

flutter: *** API Request - Start ***
flutter: URI: http://.../api/Profile/GetUserProfile
flutter: METHOD: GET
flutter: HEADERS:
flutter:  - content-type: application/json; charset=utf-8
flutter:  - Authorization: Bearer --omitted--
flutter: BODY:
flutter: 
flutter: *** API Request - End ***
flutter: *** Api Response - Start ***
flutter: URI: http://.../api/Profile/GetUserProfile
flutter: STATUS CODE: 200
flutter: REDIRECT: false
flutter: BODY:
flutter: {...}
flutter: *** Api Response - End ***

@devsideal
Copy link

Its showing syntax error?

'LoggingInterceptor.onRequest' ('Future Function(RequestOptions)') isn't a valid override of 'Interceptor.onRequest' ('void Function(RequestOptions, RequestInterceptorHandler)').

@apgapg
Copy link
Author

apgapg commented Oct 4, 2021

Yes, we need to provide an update to this gist as there were breaking changes in dio v4.0.0+

@sezabass
Copy link

sezabass commented Oct 6, 2021

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