logRequest function

void logRequest({
  1. required TrayRequest request,
  2. required FetchTrayLogLevel logType,
  3. required Response response,
  4. FetchTrayDebugLevel? requestDebugLevel,
  5. String? message,
  6. StackTrace? stackTrace,
})

provides a shortcut to logging out requests

Implementation

void logRequest({
  required TrayRequest request,
  required FetchTrayLogLevel logType,
  required http.Response response,
  FetchTrayDebugLevel? requestDebugLevel,
  String? message,
  StackTrace? stackTrace,
}) async {
  final shouldBeShown = (request
      .getEnvironment()
      .showDebugInfo(logType: logType, localDebugLevel: requestDebugLevel));

  // if should not be shown -> do nothing
  if (shouldBeShown == false) {
    return;
  }

  var logger = Logger();

  // await values
  final url = Uri.parse(await request.getUrlWithParams());
  final headers = await request.getHeaders();
  final body = await request.getBody();

  // define request details:
  var encoder = const JsonEncoder.withIndent("     ");
  final requestDetails = encoder.convert(
    {
      'requestUrl': request.url,
      'method': request.method.toString(),
      'request': url.toString(),
      'headers': headers,
      'body': body,
      'resultBody': response.body,
    },
  );

  // if we have an error -> show error logging
  switch (logType) {
    case FetchTrayLogLevel.info:
      logger.i('${message ?? 'FetchTray Info'}\n\n$requestDetails');
      break;
    case FetchTrayLogLevel.warning:
      logger.w(requestDetails, message ?? 'FetchTray Warning', stackTrace);
      break;
    case FetchTrayLogLevel.error:
      logger.e(requestDetails, message ?? 'FetchTray Error', stackTrace);
      break;
  }
}