Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ HttpLoggingInterceptor: allow to pass custom logger #470

Merged
merged 1 commit into from
Aug 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions chopper/lib/src/http_logging_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:chopper/src/request.dart';
import 'package:chopper/src/response.dart';
import 'package:chopper/src/utils.dart';
import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';

enum Level {
Expand Down Expand Up @@ -71,11 +72,13 @@ enum Level {
@immutable
class HttpLoggingInterceptor
implements RequestInterceptor, ResponseInterceptor {
const HttpLoggingInterceptor({this.level = Level.body})
: _logBody = level == Level.body,
HttpLoggingInterceptor({this.level = Level.body, Logger? logger})
techouse marked this conversation as resolved.
Show resolved Hide resolved
: _logger = logger ?? chopperLogger,
_logBody = level == Level.body,
_logHeaders = level == Level.body || level == Level.headers;

final Level level;
final Logger _logger;
final bool _logBody;
final bool _logHeaders;

Expand All @@ -97,25 +100,25 @@ class HttpLoggingInterceptor
}

// Always start on a new line
chopperLogger.info('');
chopperLogger.info(startRequestMessage);
_logger.info('');
_logger.info(startRequestMessage);

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

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

if (_logBody && bodyMessage.isNotEmpty) {
chopperLogger.info('');
chopperLogger.info(bodyMessage);
_logger.info('');
_logger.info(bodyMessage);
}

if (_logHeaders || _logBody) {
chopperLogger.info('--> END ${base.method}');
_logger.info('--> END ${base.method}');
}

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

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

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

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

if (_logBody && bodyMessage.isNotEmpty) {
chopperLogger.info('');
chopperLogger.info(bodyMessage);
_logger.info('');
_logger.info(bodyMessage);
}

if (_logBody || _logHeaders) {
chopperLogger.info('<-- END HTTP');
_logger.info('<-- END HTTP');
}

return response;
Expand Down