Skip to content

Commit

Permalink
✨ Attach request/response to interceptor log lines (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxr1998 authored Aug 10, 2023
1 parent 0af17f8 commit 1a01b60
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
13 changes: 13 additions & 0 deletions chopper/lib/src/chopper_log_record.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:chopper/src/request.dart';
import 'package:chopper/src/response.dart';

class ChopperLogRecord {
const ChopperLogRecord(this.message, {this.request, this.response});

final String message;
final Request? request;
final Response? response;

@override
String toString() => message;
}
45 changes: 30 additions & 15 deletions chopper/lib/src/http_logging_interceptor.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:chopper/src/chopper_log_record.dart';
import 'package:chopper/src/interceptor.dart';
import 'package:chopper/src/request.dart';
import 'package:chopper/src/response.dart';
Expand Down Expand Up @@ -100,25 +101,33 @@ class HttpLoggingInterceptor
}

// Always start on a new line
_logger.info('');
_logger.info(startRequestMessage);
_logger.info(ChopperLogRecord('', request: request));
_logger.info(ChopperLogRecord(startRequestMessage, request: request));

if (_logHeaders) {
base.headers.forEach((k, v) => _logger.info('$k: $v'));
base.headers.forEach(
(k, v) => _logger.info(ChopperLogRecord('$k: $v', request: request)),
);

if (base.contentLength != null &&
base.headers['content-length'] == null) {
_logger.info('content-length: ${base.contentLength}');
_logger.info(ChopperLogRecord(
'content-length: ${base.contentLength}',
request: request,
));
}
}

if (_logBody && bodyMessage.isNotEmpty) {
_logger.info('');
_logger.info(bodyMessage);
_logger.info(ChopperLogRecord('', request: request));
_logger.info(ChopperLogRecord(bodyMessage, request: request));
}

if (_logHeaders || _logBody) {
_logger.info('--> END ${base.method}');
_logger.info(ChopperLogRecord(
'--> END ${base.method}',
request: request,
));
}

return request;
Expand Down Expand Up @@ -148,27 +157,33 @@ class HttpLoggingInterceptor
}

// Always start on a new line
_logger.info('');
_logger.info(
_logger.info(ChopperLogRecord('', response: response));
_logger.info(ChopperLogRecord(
'<-- $reasonPhrase ${base.request?.method} ${base.request?.url.toString()}$bytes',
);
response: response,
));

if (_logHeaders) {
base.headers.forEach((k, v) => _logger.info('$k: $v'));
base.headers.forEach(
(k, v) => _logger.info(ChopperLogRecord('$k: $v', response: response)),
);

if (base.contentLength != null &&
base.headers['content-length'] == null) {
_logger.info('content-length: ${base.contentLength}');
_logger.info(ChopperLogRecord(
'content-length: ${base.contentLength}',
response: response,
));
}
}

if (_logBody && bodyMessage.isNotEmpty) {
_logger.info('');
_logger.info(bodyMessage);
_logger.info(ChopperLogRecord('', response: response));
_logger.info(ChopperLogRecord(bodyMessage, response: response));
}

if (_logBody || _logHeaders) {
_logger.info('<-- END HTTP');
_logger.info(ChopperLogRecord('<-- END HTTP', response: response));
}

return response;
Expand Down

0 comments on commit 1a01b60

Please sign in to comment.