Skip to content

Commit 3000c2e

Browse files
stainless-app[bot]stainless-bot
authored andcommitted
feat(client): add logging when debug env is set (#124)
1 parent c08609a commit 3000c2e

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
@@ -322,6 +322,22 @@ get a map of untyped fields of type `Map<String, JsonValue>`. You can then acces
322322
`._additionalProperties().get("secret_prop").asString()` or use other helpers defined on the `JsonValue` class
323323
to extract it to a desired type.
324324

325+
## Logging
326+
327+
We use the standard [OkHttp logging interceptor](https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor).
328+
329+
You can enable logging by setting the environment variable `ORB_LOG` to `info`.
330+
331+
```sh
332+
$ export ORB_LOG=info
333+
```
334+
335+
Or to `debug` for more verbose logging.
336+
337+
```sh
338+
$ export ORB_LOG=debug
339+
```
340+
325341
## Semantic versioning
326342

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

orb-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(":orb-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")

orb-java-client-okhttp/src/main/kotlin/com/withorb/api/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("ORB_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)