Skip to content

Commit

Permalink
slf4j: allow providing of LOGGER_CLASS via .env file if `Config.u…
Browse files Browse the repository at this point in the history
…seEnvFile()` is called before any logging calls #92
  • Loading branch information
angryziber committed Dec 13, 2024
1 parent da102cf commit 0f3f901
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions sample/.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
LOGGER_CLASS=klite.slf4j.StackTraceOptimizingJsonLogger

DB_URL=jdbc:postgresql://localhost:65432/app
DB_USER=app
DB_PASS=app
2 changes: 1 addition & 1 deletion sample/src/Launcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonBody>() // enables parsing/sending of application/json requests/responses, depending on the Accept header

if (Config.isDev) startDevDB() // start docker-compose db automatically
Expand Down
2 changes: 1 addition & 1 deletion slf4j/src/KliteLoggerFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import java.util.concurrent.ConcurrentHashMap

class KliteLoggerFactory: ILoggerFactory {
private val loggers = ConcurrentHashMap<String, Logger>()
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<Logger>
override fun getLogger(name: String) = loggers.getOrPut(name) { loggerConstructor.newInstance(name) }
}
2 changes: 1 addition & 1 deletion slf4j/src/StackTraceOptimizingJsonLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {
Expand Down
2 changes: 1 addition & 1 deletion slf4j/src/StackTraceOptimizingLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0f3f901

Please sign in to comment.