-
-
Notifications
You must be signed in to change notification settings - Fork 121
add support for logging streamed data in http_logger #497
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,3 +1,5 @@ | ||||||||||||||||||||||||
| import 'dart:convert'; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import 'package:http_interceptor/http_interceptor.dart'; | ||||||||||||||||||||||||
| import 'package:talker/talker.dart'; | ||||||||||||||||||||||||
| import 'package:talker_http_logger/http_error_log.dart'; | ||||||||||||||||||||||||
|
|
@@ -103,32 +105,63 @@ class TalkerHttpLogger extends InterceptorContract { | |||||||||||||||||||||||
| }) async { | ||||||||||||||||||||||||
| final String message = '${response.request?.url}'; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| switch (response.statusCode) { | ||||||||||||||||||||||||
| BaseResponse resForTalker; | ||||||||||||||||||||||||
| BaseResponse resForReturn; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (response is StreamedResponse) { | ||||||||||||||||||||||||
| final bytes = await response.stream.toBytes(); | ||||||||||||||||||||||||
| final body = utf8.decode(bytes); | ||||||||||||||||||||||||
| resForTalker = Response( | ||||||||||||||||||||||||
| body, | ||||||||||||||||||||||||
| response.statusCode, | ||||||||||||||||||||||||
| headers: response.headers, | ||||||||||||||||||||||||
| isRedirect: response.isRedirect, | ||||||||||||||||||||||||
| persistentConnection: response.persistentConnection, | ||||||||||||||||||||||||
| reasonPhrase: response.reasonPhrase, | ||||||||||||||||||||||||
| request: response.request, | ||||||||||||||||||||||||
|
Comment on lines
+112
to
+121
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (performance): Buffering the full streamed response body may be problematic for large payloads. Reading the entire Suggested implementation: To make this behavior configurable (as hinted in the review), you may also want to:
|
||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
Comment on lines
+114
to
+123
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question (bug_risk): Re-wrapping For streamed responses, |
||||||||||||||||||||||||
| resForReturn = StreamedResponse( | ||||||||||||||||||||||||
| Stream.value(bytes), | ||||||||||||||||||||||||
| response.statusCode, | ||||||||||||||||||||||||
| headers: response.headers, | ||||||||||||||||||||||||
| isRedirect: response.isRedirect, | ||||||||||||||||||||||||
| persistentConnection: response.persistentConnection, | ||||||||||||||||||||||||
| reasonPhrase: response.reasonPhrase, | ||||||||||||||||||||||||
| request: response.request, | ||||||||||||||||||||||||
| contentLength: bytes.length, | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
|
Comment on lines
+130
to
+133
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (bug_risk): Using The original
Suggested change
|
||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| resForTalker = response; | ||||||||||||||||||||||||
| resForReturn = response; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| switch (resForTalker.statusCode) { | ||||||||||||||||||||||||
| case int statusCode when settings.enabled && statusCode < 400: | ||||||||||||||||||||||||
| if (settings.responseFilter?.call(response) ?? true) { | ||||||||||||||||||||||||
| if (settings.responseFilter?.call(resForTalker) ?? true) { | ||||||||||||||||||||||||
| _talker.logCustom( | ||||||||||||||||||||||||
| HttpResponseLog( | ||||||||||||||||||||||||
| message, | ||||||||||||||||||||||||
| response: response, | ||||||||||||||||||||||||
| response: resForTalker, | ||||||||||||||||||||||||
| settings: settings, | ||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||
| case _ when settings.enabled: | ||||||||||||||||||||||||
| if (settings.errorFilter?.call(response) ?? true) { | ||||||||||||||||||||||||
| if (settings.errorFilter?.call(resForTalker) ?? true) { | ||||||||||||||||||||||||
| _talker.logCustom( | ||||||||||||||||||||||||
| HttpErrorLog( | ||||||||||||||||||||||||
| message, | ||||||||||||||||||||||||
| request: response.request, | ||||||||||||||||||||||||
| response: response, | ||||||||||||||||||||||||
| request: resForTalker.request, | ||||||||||||||||||||||||
| response: resForTalker, | ||||||||||||||||||||||||
| settings: settings, | ||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return response; | ||||||||||||||||||||||||
| return resForReturn; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: Consider handling non-UTF8 / binary responses more defensively when decoding.
utf8.decode(bytes)will throw on non‑UTF8 bodies (e.g. binary or malformed payloads), causing the interceptor to fail instead of just logging. Consider usingutf8.decode(bytes, allowMalformed: true)or wrapping the decode in try/catch and falling back to a safer representation (e.g. hex orbytes.toString()) when decoding fails.