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

Conditionally-logging interceptor sample #4159

Merged
merged 1 commit into from
May 29, 2024

Conversation

JakeWharton
Copy link
Member

Closes #4156


  • CHANGELOG.md's "Unreleased" section has been updated, if applicable.

@JakeWharton
Copy link
Member Author

Output:

May 29, 2024 11:40:53 AM okhttp3.mockwebserver.MockWebServer$3 execute
INFO: MockWebServer[63842] starting to accept connections
May 29, 2024 11:40:53 AM okhttp3.mockwebserver.MockWebServer$4 processOneRequest
INFO: MockWebServer[63842] received request: GET /one HTTP/1.1 and responded: HTTP/1.1 200 OK
May 29, 2024 11:40:53 AM okhttp3.mockwebserver.MockWebServer$4 processOneRequest
INFO: MockWebServer[63842] received request: GET /two HTTP/1.1 and responded: HTTP/1.1 200 OK
--> GET http://localhost:63842/two
--> END GET
<-- 200 OK http://localhost:63842/two (0ms)
Content-Length: 0
<-- END HTTP (0-byte body)

* Retrofit service functions which are annotated with this class will have their HTTP calls
* logged. You must add [ConditionalLoggingInterceptor] to your [OkHttpClient] for this to work.
*/
annotation class LogInterceptor

Choose a reason for hiding this comment

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

nit: Should this just be Log? It's kind of weird to think about interceptors as a concept in your service definition.

Copy link
Member Author

Choose a reason for hiding this comment

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

I started with Log and it looked weird, but I don't care. Put back.

override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
request.tag(Invocation::class.java)?.let { invocation ->
if (invocation.method().isAnnotationPresent(LogInterceptor::class.java)) {

Choose a reason for hiding this comment

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

Does the annotation need to have a specific retention for this to reliably work? Also, only an Android issue, but would r8 rules be needed as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

The default is runtime retention which should be fine. These are only samples so R8 is an exercise left for the reader. It should generally be okay without any rules, I think.

@JakeWharton JakeWharton force-pushed the jw.conditional-interceptor.2024-05-29 branch from 15827f9 to 8c2b181 Compare May 29, 2024 16:48
@JakeWharton JakeWharton merged commit e104ab6 into trunk May 29, 2024
7 checks passed
@JakeWharton JakeWharton deleted the jw.conditional-interceptor.2024-05-29 branch May 29, 2024 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Set logging interceptor based on API
2 participants