Don't log OkHttp binary bodies in debug builds (#7919)

This is mostly done when uploading bug reports and on one hand, it doesn't really make sense since the binary contents will be unreadable anyway, and on the other it also messes up with our debug tools, making debugging some issues harder.
This commit is contained in:
Jorge Martin Espinosa
2026-10-14 14:04:07 +02:00
committed by GitHub
parent 566eb1f4f3
commit 3ef59a8910
@@ -10,6 +10,7 @@ package io.element.android.libraries.network.interceptors
import dev.zacsweers.metro.AppScope import dev.zacsweers.metro.AppScope
import dev.zacsweers.metro.Inject import dev.zacsweers.metro.Inject
import dev.zacsweers.metro.SingleIn import dev.zacsweers.metro.SingleIn
import io.element.android.libraries.core.mimetype.MimeTypes
import io.element.android.libraries.matrix.api.tracing.LogLevel import io.element.android.libraries.matrix.api.tracing.LogLevel
import io.element.android.libraries.preferences.api.store.AppPreferencesStore import io.element.android.libraries.preferences.api.store.AppPreferencesStore
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
@@ -31,7 +32,12 @@ class DynamicHttpLoggingInterceptor(
override fun intercept(chain: Interceptor.Chain): Response { override fun intercept(chain: Interceptor.Chain): Response {
// This is called in a separate thread, so calling `runBlocking` here should be fine, it should be also instant after the value is cached // This is called in a separate thread, so calling `runBlocking` here should be fine, it should be also instant after the value is cached
val logLevel = runBlocking { appPreferencesStore.getTracingLogLevelFlow().first() } val logLevel = runBlocking { appPreferencesStore.getTracingLogLevelFlow().first() }
loggingInterceptor.level = if (logLevel >= LogLevel.DEBUG) Level.BODY else Level.NONE // If the request body is binary, we log only the headers to avoid logging binary data
val isBinaryBody = chain.request().headers["Content-Type"] == MimeTypes.OctetStream
loggingInterceptor.level = when {
logLevel >= LogLevel.DEBUG -> if (isBinaryBody) Level.HEADERS else Level.BODY
else -> Level.NONE
}
return loggingInterceptor.intercept(chain) return loggingInterceptor.intercept(chain)
} }
} }