From 11e242709d23966bba3702b194b85ae0bed2dd41 Mon Sep 17 00:00:00 2001 From: Henner Date: Fri, 20 Sep 2024 12:36:11 +0200 Subject: [PATCH] Use Reflection instead of a Stacktrace for HttpClient Logger name Add test for logger naming in TypedHttpClient Use Kotlin Reflection instead of an Exceptions Stacktrace Fix JsonHttpClientTest to assert on JsonHTTPClient name, not test class name --- core/src/http/TypedHttpClient.kt | 5 +---- core/test/TypedHttpClientTest.kt | 30 ++++++++++++++++++++++++++++++ jackson/test/JsonHttpClientTest.kt | 2 +- 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 core/test/TypedHttpClientTest.kt diff --git a/core/src/http/TypedHttpClient.kt b/core/src/http/TypedHttpClient.kt index 96b90109..6eabe5b0 100644 --- a/core/src/http/TypedHttpClient.kt +++ b/core/src/http/TypedHttpClient.kt @@ -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 @@ -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") diff --git a/core/test/TypedHttpClientTest.kt b/core/test/TypedHttpClientTest.kt new file mode 100644 index 00000000..bdd43a84 --- /dev/null +++ b/core/test/TypedHttpClientTest.kt @@ -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 = "", +) diff --git a/jackson/test/JsonHttpClientTest.kt b/jackson/test/JsonHttpClientTest.kt index 506e04ce..5a9d3267 100644 --- a/jackson/test/JsonHttpClientTest.kt +++ b/jackson/test/JsonHttpClientTest.kt @@ -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() {