From 0f3f901772f11f7ba1e4fa0a1e04bb093f7d0b71 Mon Sep 17 00:00:00 2001 From: Anton Keks Date: Fri, 13 Dec 2024 20:17:13 +0200 Subject: [PATCH] slf4j: allow providing of `LOGGER_CLASS` via `.env` file if `Config.useEnvFile()` is called before any logging calls #92 --- CHANGELOG.md | 1 + sample/.env | 2 ++ sample/src/Launcher.kt | 2 +- slf4j/src/KliteLoggerFactory.kt | 2 +- slf4j/src/StackTraceOptimizingJsonLogger.kt | 2 +- slf4j/src/StackTraceOptimizingLogger.kt | 2 +- 6 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa4faf12..610fa685 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * jdbc: allow calling of `PooledConnection.close()` multiple times #80 * json: fix TSGenerator on Windows * json: TSGenerator will now use more type-safe string template types for java.time classes, e.g. `${number}-${number}-${number}` instead of `string` +* slf4j: allow providing of `LOGGER_CLASS` via `.env` file if `Config.useEnvFile()` is called before any logging calls #92 * jackson: serialize enums using their toString() method by default, this fixes `openapi` module usage with `jackson` #88 * liquibase: do not close jdbc connection if it was passed by user #81 diff --git a/sample/.env b/sample/.env index 5c63cb29..7f5e8794 100644 --- a/sample/.env +++ b/sample/.env @@ -1,3 +1,5 @@ +LOGGER_CLASS=klite.slf4j.StackTraceOptimizingJsonLogger + DB_URL=jdbc:postgresql://localhost:65432/app DB_USER=app DB_PASS=app diff --git a/sample/src/Launcher.kt b/sample/src/Launcher.kt index 1858df07..fdb154ba 100644 --- a/sample/src/Launcher.kt +++ b/sample/src/Launcher.kt @@ -14,11 +14,11 @@ import java.nio.file.Path import java.time.Duration.ofSeconds fun main() { + Config.useEnvFile() sampleServer().start() } fun sampleServer(port: Int = 8080) = Server(listen = InetSocketAddress(port)).apply { - Config.useEnvFile() use() // enables parsing/sending of application/json requests/responses, depending on the Accept header if (Config.isDev) startDevDB() // start docker-compose db automatically diff --git a/slf4j/src/KliteLoggerFactory.kt b/slf4j/src/KliteLoggerFactory.kt index db68a9d0..1e239d7d 100644 --- a/slf4j/src/KliteLoggerFactory.kt +++ b/slf4j/src/KliteLoggerFactory.kt @@ -8,7 +8,7 @@ import java.util.concurrent.ConcurrentHashMap class KliteLoggerFactory: ILoggerFactory { private val loggers = ConcurrentHashMap() - private val loggerConstructor = findConstructor(Config.optional("LOGGER_CLASS", KliteLogger::class.java.name)) + private val loggerConstructor by lazy { findConstructor(Config.optional("LOGGER_CLASS", KliteLogger::class.java.name)) } @Suppress("UNCHECKED_CAST") internal fun findConstructor(className: String) = Class.forName(className).getConstructor(String::class.java) as Constructor override fun getLogger(name: String) = loggers.getOrPut(name) { loggerConstructor.newInstance(name) } } diff --git a/slf4j/src/StackTraceOptimizingJsonLogger.kt b/slf4j/src/StackTraceOptimizingJsonLogger.kt index 14b8e9d9..e3ee95fc 100644 --- a/slf4j/src/StackTraceOptimizingJsonLogger.kt +++ b/slf4j/src/StackTraceOptimizingJsonLogger.kt @@ -2,7 +2,7 @@ package klite.slf4j /** * Use this logger if you want stack traces to be logged as a single log line in json format: - * Config["LOGGER_CLASS"] = StacktraceOptimizingJsonLogger::class + * `Config["LOGGER_CLASS"] = StackTraceOptimizingJsonLogger::class.qualifiedName!!` */ open class StackTraceOptimizingJsonLogger(name: String): StackTraceOptimizingLogger(name) { override fun print(formatted: String, t: Throwable?) { diff --git a/slf4j/src/StackTraceOptimizingLogger.kt b/slf4j/src/StackTraceOptimizingLogger.kt index 4c79974f..aece114e 100644 --- a/slf4j/src/StackTraceOptimizingLogger.kt +++ b/slf4j/src/StackTraceOptimizingLogger.kt @@ -2,7 +2,7 @@ package klite.slf4j /** * Use this logger if you want shorter stack traces: - * Config["LOGGER_CLASS"] = StacktraceOptimizingLogger::class + * `Config["LOGGER_CLASS"] = StackTraceOptimizingLogger::class.qualifiedName!!` */ open class StackTraceOptimizingLogger(name: String): KliteLogger(name) { public override fun print(formatted: String, t: Throwable?): Unit = synchronized(out) {