Skip to content

Instantly share code, notes, and snippets.

@bizz84
Created May 13, 2024 07:38
Show Gist options
  • Save bizz84/f5d042f4a9f57a30d379205ac2e306f7 to your computer and use it in GitHub Desktop.
Save bizz84/f5d042f4a9f57a30d379205ac2e306f7 to your computer and use it in GitHub Desktop.
Simple logger interceptor for Dio without extra dependencies
import 'dart:developer';
import 'package:dio/dio.dart';
/// A simple interceptor used to log all network requests
/// For more details, see: https://github.com/bizz84/flutter-tips-and-tricks/blob/main/tips/0152-log-status-code-emoji/index.md
class LoggerInterceptor implements Interceptor {
final stopwatches = <String, Stopwatch>{};
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
final url = '${options.baseUrl}${options.path}';
stopwatches[url] = Stopwatch()..start();
log('🌍 Making request: $url');
return handler.next(options);
}
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
final url =
'${response.requestOptions.baseUrl}${response.requestOptions.path}';
_logMessageAndClearStopwatch(
response.statusCode, url, '⬅️ Received response');
if (response.requestOptions.queryParameters.isNotEmpty) {
log('Query params: ${response.requestOptions.queryParameters}');
}
log('-------------------------');
return handler.next(response);
}
@override
void onError(DioException err, ErrorInterceptorHandler handler) {
final url = err.requestOptions.uri.toString();
_logMessageAndClearStopwatch(null, url, '❌ Received error');
log('${err.stackTrace}');
if (err.response?.data != null) {
log('❌ Response Error: ${err.response?.data}');
}
return handler.next(err);
}
void _logMessageAndClearStopwatch(
int? statusCode, String url, String message) {
final stopwatch = stopwatches[url];
if (stopwatch != null) {
stopwatch.stop();
_logResponse(statusCode, stopwatch.elapsedMilliseconds, url);
stopwatches.remove(url);
} else {
log(message);
}
}
void _logResponse(int? statusCode, int milliseconds, String url) {
final emoji = switch (statusCode) {
!= null && >= 200 && < 300 => '✅',
!= null && >= 300 && < 400 => '🟠',
_ => '❌'
};
if (statusCode != null) {
log('$emoji $statusCode $emoji | ${milliseconds}ms | $url');
} else {
log('$emoji | ${milliseconds}ms | $url');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment