-
Notifications
You must be signed in to change notification settings - Fork 0
feature/enable-custom-http-logger-interceptor #23
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
feature/enable-custom-http-logger-interceptor #23
Conversation
… feature/enable-custom-http-logger-interceptor
|
Let's take the opportunity to replace
Otherwise the new interceptor should follow the same features as Retrofit has, including log levels and header suppression. Let's try and fit the entire log in a single log entry. |
… feature/enable-custom-http-logger-interceptor
|
|
||
| sb.append(getRequestDescription(request, connection)); | ||
|
|
||
| if (loggingLevel.getLevel() >= 2) { |
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.
For readability reasons I would've written like this
if (loggingLevel.getLevel() >= LoggingLevel.HEADERS.getLevel()) ...
It would've been even better to write something like this
if (loggingLevel.includes(LoggingLevel.HEADERS))
| } | ||
|
|
||
| private OkHttpClient.Builder createDefaultBuilder(HttpLoggingInterceptor loggingInterceptor) { | ||
| private OkHttpClient.Builder createDefaultBuilder(Interceptor loggingInterceptor) { |
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.
Probably should be LoggingInterceptor otherwise someone might misuse it.
| sb.append("\n").append(getRequestHeaders(request)); | ||
| } | ||
|
|
||
| if (loggingLevel.getLevel() >= 3) { |
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.
Same here
if (loggingLevel.getLevel() >= LoggingLevel.BODY.getLevel()) ...
It would've been even better to write something like this
if (loggingLevel.includes(LoggingLevel.BODY))
|
|
||
| for (int i = 0; i < requestHeaders.size(); i++) { | ||
| var name = requestHeaders.name(i); | ||
| if (List.of("content-type", "content-length").contains(name.toLowerCase())) { |
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.
In some places we use constants, others we hardcode. Maybe we should follow same convention - use constants
|
|
||
| for (int i = 0; i < responseHeaders.size(); i++) { | ||
| var name = responseHeaders.name(i); | ||
| if (List.of("content-type", "content-length").contains(name.toLowerCase())) { |
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.
In some places we use constants, others we hardcode. Maybe we should follow same convention - use constants
| } | ||
|
|
||
| if (contentLength != 0L) { | ||
| return buffer.clone().readString(charset); |
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.
Maybe we should allow to limit how much of the content gets logged. If response is a 150k element json list of objects, this log entry could cause harm to log collector and become virtually unreadable.
| import static okhttp3.internal.http.HttpHeaders.promisesBody; | ||
|
|
||
| @Slf4j | ||
| public class RetrofitLoggingInterceptor implements LoggingInterceptor { |
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.
We might want to add some sort of white/blacklist logic to control for which endpoints we want to log/omit bodies.
This would allow for example to omit response bodies for requests that contain confidential data in body and handle that separately.
|
Logging level documentation sample: https://github.com/square/okhttp/blob/master/okhttp-logging-interceptor/src/main/kotlin/okhttp3/logging/HttpLoggingInterceptor.kt#L111 |
Jorich
left a comment
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.
Mostly looks promising. Just a few small fixes
| private Converter.Factory converterFactory; | ||
| private OkHttpClient.Builder clientBuilder; | ||
|
|
||
| public static <T> RetrofitApiBuilder<T> create(String baseUrl, Class<T> definition) { |
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.
Here goes the backward compatibility :)
I am not sure how many cases use retrofit api builder directly but I am afraid this change is a major version bump.
| return this; | ||
| } | ||
|
|
||
| public RetrofitApiBuilder<T> loggingLevel(LoggingLevel level) { |
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.
This also results in major version bump
| public void write(Map<String, String> container) { | ||
| Map<String, String> currentContext = MDC.getCopyOfContextMap(); | ||
|
|
||
| log(container); |
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.
I would wrap this in try {} finally {} block so that MDC wouldn't be left unswapped
Remove some non-standard mappers Add no-op interceptor for when logging has been turned off All mappers are extendable and mapper beans can be overridden
Add tests
| return new RetrofitLoggingInterceptorImplementation(mappers, new RetrofitLogLoggerWriterAdapter()); | ||
| } | ||
|
|
||
| // testida üle, kui on vajadus mapper üle kirjutada |
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.
I suppose that is done
…r-interceptor' into feature/enable-custom-http-logger-interceptor
|



No description provided.