Skip to content

An OkHttp interceptor which logs HTTP request and response data and allows output customization.

License

Notifications You must be signed in to change notification settings

bartek-wesolowski/customizable-okhttp-logging-interceptor

Repository files navigation

Customizable OkHttp Logging Interceptor

Maven Central example workflow

An OkHttp interceptor which logs HTTP request and response data and allows output customization. The implementation is based on the Logging Interceptor from OkHttp.

val loggingInterceptor = CustomizableHttpLoggingInterceptor(
    logHeaders = true,
    logBody = true,
    maxBodyLength = 1000L
)
val client = OkHttpClient.Builder()
    .addInterceptor(loggingInterceptor)
    .build()

To log to a custom location, pass a Logger instance to the constructor.

val loggingInterceptor = CustomizableHttpLoggingInterceptor(
    logger = { message -> Timber.tag("OkHttp").d(message) }
)

Warning: The logs generated by this interceptor when using the HEADERS or BODY levels have the potential to leak sensitive information such as "Authorization" or "Cookie" headers and the contents of request and response bodies. This data should only be logged in a controlled way or in a non-production environment.

You can redact headers that may contain sensitive information by calling redactHeader().

loggingInterceptor.redactHeader("Authorization")
loggingInterceptor.redactHeader("Cookie")

Installation

Customizable OkHttp Logging Interceptor is available on Maven Central. Add the following to your build.gradle/build.gradle.kts file:

Groovy DSL

testImplementation "io.github.bartek-wesolowski:customizable-okhttp-logging-interceptor:2.0.0"

Kotlin DSL

testImplementation("io.github.bartek-wesolowski:customizable-okhttp-logging-interceptor:2.0.0")

About

An OkHttp interceptor which logs HTTP request and response data and allows output customization.

Topics

Resources

License

Stars

Watchers

Forks

Languages