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

Hide a specific header from dio logger #315

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
10 changes: 10 additions & 0 deletions packages/talker_dio_logger/lib/dio_logs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:talker/talker.dart';
import 'package:talker_dio_logger/talker_dio_logger.dart';

const _encoder = JsonEncoder.withIndent(' ');
const _hiddenValue = '*****';

class DioRequestLog extends TalkerLog {
DioRequestLog(
Expand Down Expand Up @@ -37,6 +38,15 @@ class DioRequestLog extends TalkerLog {
msg += '\nData: $prettyData';
}
if (settings.printRequestHeaders && headers.isNotEmpty) {
if (settings.hideHeaderValuesForKeys.isNotEmpty) {
headers.updateAll((key, value) {
return settings.hideHeaderValuesForKeys
.map((v) => v.toLowerCase())
.contains(key.toLowerCase())
? _hiddenValue
: value;
});
}
final prettyHeaders = _encoder.convert(headers);
msg += '\nHeaders: $prettyHeaders';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TalkerDioLoggerSettings {
this.printErrorMessage = true,
this.printRequestData = true,
this.printRequestHeaders = false,
this.hideHeaderValuesForKeys = const <String>{},
this.requestPen,
this.responsePen,
this.errorPen,
Expand Down Expand Up @@ -93,6 +94,10 @@ class TalkerDioLoggerSettings {
/// You can add your custom logic to log only specific Dio error [DioException].
final bool Function(DioException response)? errorFilter;

/// Header values for the specified keys in the Set will be replaced with *****.
/// Case insensitive
final Set<String> hideHeaderValuesForKeys;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hideHeaderValuesForKeys is huge and hard name for this field
Can we rename it to hiddenHeaders ?


TalkerDioLoggerSettings copyWith({
bool? printResponseData,
bool? printResponseHeaders,
Expand All @@ -108,6 +113,7 @@ class TalkerDioLoggerSettings {
bool Function(RequestOptions requestOptions)? requestFilter,
bool Function(Response response)? responseFilter,
bool Function(DioException response)? errorFilter,
Set<String>? hideHeaderValuesForKeys,
}) {
return TalkerDioLoggerSettings(
printResponseData: printResponseData ?? this.printResponseData,
Expand All @@ -124,6 +130,8 @@ class TalkerDioLoggerSettings {
requestFilter: requestFilter ?? this.requestFilter,
responseFilter: responseFilter ?? this.responseFilter,
errorFilter: errorFilter ?? this.errorFilter,
hideHeaderValuesForKeys:
hideHeaderValuesForKeys ?? this.hideHeaderValuesForKeys,
);
}
}
24 changes: 24 additions & 0 deletions packages/talker_dio_logger/test/logger_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,29 @@ void main() {
' ]\n'
'}');
});

test('onRequest method should hide specific header values in log', () {
final logger = TalkerDioLogger(
talker: talker,
settings: TalkerDioLoggerSettings(
printRequestHeaders: true,
hideHeaderValuesForKeys: {'Authorization'}));

final options = RequestOptions(path: '/test', headers: {
"firstHeader": "firstHeaderValue",
"authorization": "bearer super_secret_token",
"lastHeader": "lastHeaderValue",
});
logger.onRequest(options, RequestInterceptorHandler());
print(talker.history);
expect(
talker.history.last.generateTextMessage(),
'[http-request] [GET] /test\n'
'Headers: {\n'
' "firstHeader": "firstHeaderValue",\n'
' "authorization": "*****",\n'
' "lastHeader": "lastHeaderValue"\n'
'}');
});
});
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import 'package:http_interceptor/http_interceptor.dart';
import 'package:talker_http_logger/talker_http_logger.dart';
import 'package:talker_http_logger/talker_http_logger_settings.dart';

void main(List<String> args) async {
final client = InterceptedClient.build(interceptors: [
TalkerHttpLogger(),
TalkerHttpLogger(
settings: TalkerHttpLoggerSettings(
hideHeaderValuesForKeys: {'Authorization'})),
]);

await client.get("https://google.com".toUri());
await client.get("https://google.com".toUri(), headers: {
"firstHeader": "firstHeaderValue",
"authorization": "bearer super_secret_token",
"lastHeader": "lastHeaderValue",
});
}
29 changes: 27 additions & 2 deletions packages/talker_http_logger/lib/talker_http_logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@ import 'dart:convert';

import 'package:http_interceptor/http_interceptor.dart';
import 'package:talker/talker.dart';
import 'package:talker_http_logger/talker_http_logger_settings.dart';

class TalkerHttpLogger extends InterceptorContract {
TalkerHttpLogger({Talker? talker}) {
TalkerHttpLogger(
{Talker? talker, this.settings = const TalkerHttpLoggerSettings()}) {
_talker = talker ?? Talker();
}

late Talker _talker;

/// [TalkerHttpLogger] settings and customization
TalkerHttpLoggerSettings settings;

Comment on lines +17 to +19
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that this feature is same for talker_http and talker_dio but can we create separated PR with TalkerHttpLoggerSettings for talker_http. And hiddenHeaders will be added for TalkerHttpLoggerSettings in this new Pull-request ?

@override
Future<BaseRequest> interceptRequest({
required BaseRequest request,
}) async {
final message = '${request.url}';
_talker.logCustom(HttpRequestLog(message, request: request));
_talker.logCustom(
HttpRequestLog(message, request: request, settings: settings));
return request;
}

Expand All @@ -38,15 +44,19 @@ class TalkerHttpLogger extends InterceptorContract {
}

const encoder = JsonEncoder.withIndent(' ');
const _hiddenValue = '*****';

class HttpRequestLog extends TalkerLog {
HttpRequestLog(
String title, {
required this.request,
this.settings = const TalkerHttpLoggerSettings(),
}) : super(title);

final BaseRequest request;

final TalkerHttpLoggerSettings settings;

@override
AnsiPen get pen => (AnsiPen()..xterm(219));

Expand All @@ -62,6 +72,15 @@ class HttpRequestLog extends TalkerLog {

try {
if (headers.isNotEmpty) {
if (settings.hideHeaderValuesForKeys.isNotEmpty) {
headers.updateAll((key, value) {
return settings.hideHeaderValuesForKeys
.map((v) => v.toLowerCase())
.contains(key.toLowerCase())
? _hiddenValue
: value;
});
}
final prettyHeaders = encoder.convert(headers);
msg += '\nHeaders: $prettyHeaders';
}
Expand All @@ -76,10 +95,13 @@ class HttpResponseLog extends TalkerLog {
HttpResponseLog(
String title, {
required this.response,
this.settings = const TalkerHttpLoggerSettings(),
}) : super(title);

final BaseResponse response;

final TalkerHttpLoggerSettings settings;

@override
AnsiPen get pen => (AnsiPen()..xterm(46));

Expand Down Expand Up @@ -111,10 +133,13 @@ class HttpErrorLog extends TalkerLog {
HttpErrorLog(
String title, {
required this.response,
this.settings = const TalkerHttpLoggerSettings(),
}) : super(title);

final BaseResponse response;

final TalkerHttpLoggerSettings settings;

@override
AnsiPen get pen => AnsiPen()..red();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class TalkerHttpLoggerSettings {
const TalkerHttpLoggerSettings({
this.hideHeaderValuesForKeys = const <String>{},
});

/// Header values for the specified keys in the Set will be replaced with *****.
/// Case insensitive
final Set<String> hideHeaderValuesForKeys;
}
Loading