Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Reflection instead of a Stacktrace for HttpClient Logger name #83

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions core/src/http/TypedHttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package klite.http

import klite.error
import klite.info
import klite.logger
import kotlinx.coroutines.delay
import kotlinx.coroutines.future.await
import java.io.IOException
Expand Down Expand Up @@ -32,9 +31,7 @@ open class TypedHttpClient(
val contentType: String
) {
protected var trimToLog: String.() -> String = { if (length <= maxLoggedLen) this else substring(0, maxLoggedLen) + "…" }
var logger = logger(Exception().stackTrace.first { it.className != TypedHttpClient::class.java.name && it.className !== javaClass.name }.className).apply {
if (urlPrefix.isNotEmpty()) info("Using $urlPrefix")
}
var logger = System.getLogger(this::class.java.name).apply { if (urlPrefix.isNotEmpty()) info("Using $urlPrefix") }

private fun buildReq(urlSuffix: String) = HttpRequest.newBuilder().uri(URI("$urlPrefix$urlSuffix"))
.setHeader("Content-Type", "application/json; charset=UTF-8").setHeader("Accept", "application/json")
Expand Down
30 changes: 30 additions & 0 deletions core/test/TypedHttpClientTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package klite


import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.atrium.api.verbs.expect
import klite.http.TypedHttpClient
import org.junit.jupiter.api.Test
import java.net.http.HttpClient

class TypedHttpClientLoggerNameTest {
@Test fun loggerNameShouldReferToClassName() {
expect(
TypedHttpClient(
urlPrefix = "",
http = HttpClient.newHttpClient(),
contentType = ""
).logger.name
).toEqual("klite.http.TypedHttpClient")
}

@Test fun loggerNameShouldReferToDerivedClassName() {
expect(DerivedTypedHttpClient().logger.name).toEqual("klite.DerivedTypedHttpClient")
}
}

private class DerivedTypedHttpClient: TypedHttpClient(
urlPrefix = "",
http = HttpClient.newHttpClient(),
contentType = "",
)
2 changes: 1 addition & 1 deletion jackson/test/JsonHttpClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class JsonHttpClientTest {
retryCount = 2, retryAfter = ZERO, http = httpClient, json = kliteJsonMapper())

@Test fun `logger name from stack trace`() {
expect(http.logger.name).toEqual(javaClass.name)
expect(http.logger.name).toEqual(http.javaClass.name)
}

@Test fun get() {
Expand Down