Skip to content

Commit 73cdedc

Browse files
stainless-app[bot]stainless-bot
authored andcommitted
feat(client): add logging when debug env is set (#18)
1 parent fd9e9dc commit 73cdedc

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,22 @@ get a map of untyped fields of type `Map<String, JsonValue>`. You can then acces
368368
`._additionalProperties().get("secret_prop").asString()` or use other helpers defined on the `JsonValue` class
369369
to extract it to a desired type.
370370

371+
## Logging
372+
373+
We use the standard [OkHttp logging interceptor](https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor).
374+
375+
You can enable logging by setting the environment variable `OPENAI_LOG` to `info`.
376+
377+
```sh
378+
$ export OPENAI_LOG=info
379+
```
380+
381+
Or to `debug` for more verbose logging.
382+
383+
```sh
384+
$ export OPENAI_LOG=debug
385+
```
386+
371387
## Semantic versioning
372388

373389
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:

openai-java-client-okhttp/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ dependencies {
77
api(project(":openai-java-core"))
88

99
implementation("com.squareup.okhttp3:okhttp:4.12.0")
10+
implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")
1011

1112
testImplementation(kotlin("test"))
1213
testImplementation("org.assertj:assertj-core:3.25.3")

openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OkHttpClient.kt

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,38 @@ import okhttp3.Request
2323
import okhttp3.RequestBody
2424
import okhttp3.RequestBody.Companion.toRequestBody
2525
import okhttp3.Response
26+
import okhttp3.logging.HttpLoggingInterceptor
2627
import okio.BufferedSink
2728

2829
class OkHttpClient
2930
private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val baseUrl: HttpUrl) :
3031
HttpClient {
3132

3233
private fun getClient(requestOptions: RequestOptions): okhttp3.OkHttpClient {
33-
val timeout = requestOptions.timeout ?: return okHttpClient
34-
return okHttpClient
35-
.newBuilder()
36-
.connectTimeout(timeout)
37-
.readTimeout(timeout)
38-
.writeTimeout(timeout)
39-
.callTimeout(if (timeout.seconds == 0L) timeout else timeout.plusSeconds(30))
40-
.build()
34+
val clientBuilder = okHttpClient.newBuilder()
35+
36+
val logLevel =
37+
when (System.getenv("OPENAI_LOG")?.lowercase()) {
38+
"info" -> HttpLoggingInterceptor.Level.BASIC
39+
"debug" -> HttpLoggingInterceptor.Level.BODY
40+
else -> null
41+
}
42+
if (logLevel != null) {
43+
clientBuilder.addNetworkInterceptor(
44+
HttpLoggingInterceptor().setLevel(logLevel).apply { redactHeader("Authorization") }
45+
)
46+
}
47+
48+
val timeout = requestOptions.timeout
49+
if (timeout != null) {
50+
clientBuilder
51+
.connectTimeout(timeout)
52+
.readTimeout(timeout)
53+
.writeTimeout(timeout)
54+
.callTimeout(if (timeout.seconds == 0L) timeout else timeout.plusSeconds(30))
55+
}
56+
57+
return clientBuilder.build()
4158
}
4259

4360
override fun execute(

0 commit comments

Comments
 (0)