-
Notifications
You must be signed in to change notification settings - Fork 123
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
✨ HttpLoggingInterceptor: allow to pass custom logger #470
Conversation
It might be better to change the constructor in order to allow passing a custom class HttpLoggingInterceptor implements RequestInterceptor, ResponseInterceptor {
HttpLoggingInterceptor({this.level = Level.body, Logger? logger})
: _logBody = level == Level.body,
_logHeaders = level == Level.body || level == Level.headers,
_logger = logger ?? chopperLogger;
final Logger _logger;
/// stuff ...
} @Guldem your thoughts? |
Would be an option, but I'd prefer to have both in that case. With the extra function I'd be able to collect these lines in a single string per request, and then log them out at once - reducing unwanted interleaving of other log lines, and generally giving more flexibility. |
Codecov Report
@@ Coverage Diff @@
## develop #470 +/- ##
===========================================
+ Coverage 93.70% 93.72% +0.01%
===========================================
Files 8 8
Lines 445 446 +1
===========================================
+ Hits 417 418 +1
Misses 28 28
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
You can achieve the same by simply overriding the |
I see, I think that would also work. Let me know what you prefer and I'll implement it 😁 |
Overriding the a function feels a bit hacky. In general I think its better make the logger configurable. This should be specific to chopper. Another option could be to create a For example: class ChopperLogRecord {
const ChopperLogRecord(this.message, {this.request, this.response});
final Request? request;
final Response? response;
final String message;
@override
String toString() => message;
}
// The HttpLoggingInterceptor would log like this:
_logger.info(ChopperLogRecord('the message', request: request);
// or
_logger.info(ChopperLogRecord('the message', response: response);
// Do whats need to collect logging event from chopper.
Logger.root.onRecord.listen((event) {
if (event is ChopperLogRecord) {
_collect(event);
}
}); The hard part is when to stop collecting and print the summary ofc. Maybe print everything when the request/response is different (still not fool proof since its async). |
I like this idea! For this PR, I think just changing the interceptor to accept a custom logger would be enough, but let me know if you want me to work on the second suggestion as well. |
Also feel free to add the LogRecord suggestion 😄 |
Alright, I'll do that in a separate PR. |
LGTM |
Sure, the last few days have been a bit busy but I'll get to it later this week. |
I'm not 100% happy with this solution yet. As a workaround, one has to implement it through a delegate pattern, which gets quite messy. Do you perhaps have a suggestion for a better alternative? |
This function can then be overridden to use alternative logging implementations besides the default flutter logging.
This can be especially useful if you want to only write one (multiline) log entry per request, instead of one entry per line.