From 017aae7a1795fe933e0ea9e8ee2c8d059c6746f4 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Fri, 22 Nov 2024 16:06:54 +0000 Subject: [PATCH] feat(client): add logging when debug env is set (#18) --- README.md | 16 +++++++++ openai-java-client-okhttp/build.gradle.kts | 1 + .../com/openai/client/okhttp/OkHttpClient.kt | 33 ++++++++++++++----- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e96b6e77..9f6a7fe4 100644 --- a/README.md +++ b/README.md @@ -373,6 +373,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 `OPENAI_LOG` to `info`. + +```sh +$ export OPENAI_LOG=info +``` + +Or to `debug` for more verbose logging. + +```sh +$ export OPENAI_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/openai-java-client-okhttp/build.gradle.kts b/openai-java-client-okhttp/build.gradle.kts index d979cf9a..4f14e269 100644 --- a/openai-java-client-okhttp/build.gradle.kts +++ b/openai-java-client-okhttp/build.gradle.kts @@ -7,6 +7,7 @@ dependencies { api(project(":openai-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/openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OkHttpClient.kt b/openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OkHttpClient.kt index f4a36a33..8862168e 100644 --- a/openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OkHttpClient.kt +++ b/openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OkHttpClient.kt @@ -23,6 +23,7 @@ import okhttp3.Request import okhttp3.RequestBody import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.Response +import okhttp3.logging.HttpLoggingInterceptor import okio.BufferedSink class OkHttpClient @@ -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("OPENAI_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(