diff --git a/README.md b/README.md index e9c6176..b4c0da4 100644 --- a/README.md +++ b/README.md @@ -240,6 +240,22 @@ get a map of untyped fields of type `Map`. You can then acces `._additionalProperties().get("secret_prop").asString()` or use other helpers defined on the `JsonValue` class to extract it to a desired type. +## Logging + +We use the standard [OkHttp logging interceptor](https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor). + +You can enable logging by setting the environment variable `PRELUDE_LOG` to `info`. + +```sh +$ export PRELUDE_LOG=info +``` + +Or to `debug` for more verbose logging. + +```sh +$ export PRELUDE_LOG=debug +``` + ## Semantic versioning This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions: diff --git a/prelude-java-client-okhttp/build.gradle.kts b/prelude-java-client-okhttp/build.gradle.kts index 5cc1070..b5a5627 100644 --- a/prelude-java-client-okhttp/build.gradle.kts +++ b/prelude-java-client-okhttp/build.gradle.kts @@ -7,6 +7,7 @@ dependencies { api(project(":prelude-java-core")) implementation("com.squareup.okhttp3:okhttp:4.12.0") + implementation("com.squareup.okhttp3:logging-interceptor:4.12.0") testImplementation(kotlin("test")) testImplementation("org.assertj:assertj-core:3.25.3") diff --git a/prelude-java-client-okhttp/src/main/kotlin/so/prelude/sdk/client/okhttp/OkHttpClient.kt b/prelude-java-client-okhttp/src/main/kotlin/so/prelude/sdk/client/okhttp/OkHttpClient.kt index f68e66b..de4e1e3 100644 --- a/prelude-java-client-okhttp/src/main/kotlin/so/prelude/sdk/client/okhttp/OkHttpClient.kt +++ b/prelude-java-client-okhttp/src/main/kotlin/so/prelude/sdk/client/okhttp/OkHttpClient.kt @@ -15,6 +15,7 @@ import okhttp3.Request import okhttp3.RequestBody import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.Response +import okhttp3.logging.HttpLoggingInterceptor import okio.BufferedSink import so.prelude.sdk.core.RequestOptions import so.prelude.sdk.core.http.Headers @@ -30,14 +31,30 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val HttpClient { private fun getClient(requestOptions: RequestOptions): okhttp3.OkHttpClient { - val timeout = requestOptions.timeout ?: return okHttpClient - return okHttpClient - .newBuilder() - .connectTimeout(timeout) - .readTimeout(timeout) - .writeTimeout(timeout) - .callTimeout(if (timeout.seconds == 0L) timeout else timeout.plusSeconds(30)) - .build() + val clientBuilder = okHttpClient.newBuilder() + + val logLevel = + when (System.getenv("PRELUDE_LOG")?.lowercase()) { + "info" -> HttpLoggingInterceptor.Level.BASIC + "debug" -> HttpLoggingInterceptor.Level.BODY + else -> null + } + if (logLevel != null) { + clientBuilder.addNetworkInterceptor( + HttpLoggingInterceptor().setLevel(logLevel).apply { redactHeader("Authorization") } + ) + } + + val timeout = requestOptions.timeout + if (timeout != null) { + clientBuilder + .connectTimeout(timeout) + .readTimeout(timeout) + .writeTimeout(timeout) + .callTimeout(if (timeout.seconds == 0L) timeout else timeout.plusSeconds(30)) + } + + return clientBuilder.build() } override fun execute(