Skip to content

Commit

Permalink
Upgrade to kotlin-logging 7.0.3
Browse files Browse the repository at this point in the history
Both the artifact name and package have changed
  • Loading branch information
Gekkio committed Dec 27, 2024
1 parent 9183a2e commit 0af3277
Show file tree
Hide file tree
Showing 106 changed files with 392 additions and 322 deletions.
2 changes: 1 addition & 1 deletion service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ dependencies {

// Logging
implementation("ch.qos.logback.access:logback-access-tomcat")
implementation("io.github.microutils:kotlin-logging-jvm")
implementation("io.github.oshai:kotlin-logging-jvm")
implementation("net.logstash.logback:logstash-logback-encoder")

// Spring
Expand Down
2 changes: 1 addition & 1 deletion service/codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies {
testImplementation(platform(project(":evaka-bom")))

implementation(project(":"))
implementation("io.github.microutils:kotlin-logging-jvm")
implementation("io.github.oshai:kotlin-logging-jvm")

// Kotlin + core
implementation(kotlin("stdlib"))
Expand Down
8 changes: 4 additions & 4 deletions service/codegen/src/main/kotlin/evaka/codegen/Codegen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import evaka.codegen.actionenum.generateActionEnumTypes
import evaka.codegen.api.TsFile
import evaka.codegen.api.TsProject
import evaka.codegen.api.generateApiFiles
import io.github.oshai.kotlinlogging.KotlinLogging
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.absolute
Expand All @@ -19,15 +20,14 @@ import kotlin.io.path.readText
import kotlin.io.path.relativeTo
import kotlin.io.path.writeText
import kotlin.system.exitProcess
import mu.KotlinLogging

private val logger = KotlinLogging.logger {}

fun generate() {
val srcPath = locateFrontendSrcDirectory()
val generatedPath = srcPath / "lib-common" / "generated"
if (!generatedPath.exists()) {
logger.error("Root path $generatedPath does not exist -> aborting")
logger.error { "Root path $generatedPath does not exist -> aborting" }
exitProcess(1)
}

Expand Down Expand Up @@ -66,9 +66,9 @@ fun check() {
val currentContent = path.readText()
val relativePath = path.relativeTo(srcPath)
if (content == currentContent) {
logger.info("Generated api files up to date ($relativePath)")
logger.info { "Generated api files up to date ($relativePath)" }
} else {
logger.error("Generated api files were not up to date ($relativePath)")
logger.error { "Generated api files were not up to date ($relativePath)" }
exitProcess(1)
}
}
Expand Down
6 changes: 3 additions & 3 deletions service/codegen/src/main/kotlin/evaka/codegen/Languages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ package evaka.codegen

import fi.espoo.evaka.shared.domain.ISO_LANGUAGES_SUBSET
import fi.espoo.evaka.shared.domain.IsoLanguage
import io.github.oshai.kotlinlogging.KotlinLogging
import java.nio.file.Path
import kotlin.io.path.readText
import kotlin.io.path.writeText
import kotlin.system.exitProcess
import mu.KotlinLogging
import org.intellij.lang.annotations.Language

private val logger = KotlinLogging.logger {}

fun generateLanguages(target: Path) {
target.writeText(sequenceOf(fileHeader, languageFileBody()).joinToString("\n"))
logger.info("Generated $target")
logger.info { "Generated $target" }
}

private fun languageFileBody(): String {
Expand All @@ -42,7 +42,7 @@ private fun IsoLanguage.toTypescript(): String =

fun checkLanguages(target: Path) {
if (languageFileBody() != target.readText().skipFileHeader()) {
logger.error("File is not up to date: $target")
logger.error { "File is not up to date: $target" }
exitProcess(1)
}
}
10 changes: 5 additions & 5 deletions service/codegen/src/main/kotlin/evaka/codegen/actionenum/Cli.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

package evaka.codegen.actionenum

import io.github.oshai.kotlinlogging.KotlinLogging
import java.nio.file.Path
import kotlin.io.path.div
import kotlin.io.path.readText
import kotlin.system.exitProcess
import mu.KotlinLogging

private val logger = KotlinLogging.logger {}

Expand All @@ -17,22 +17,22 @@ fun checkGeneratedActionEnumTypes(target: Path) {
for (definition in generatedFiles) {
val file = target / definition.name
if (definition.generate() != file.readText()) {
logger.error("File is not up to date: $file")
logger.error { "File is not up to date: $file" }
errors += 1
}
}
if (errors > 0) {
logger.error("$errors were not up to date")
logger.error { "$errors were not up to date" }
exitProcess(1)
} else {
logger.info("All files up to date")
logger.info { "All files up to date" }
}
}

fun generateActionEnumTypes(target: Path) {
for (definition in generatedFiles) {
val file = target / definition.name
definition.generateTo(file)
logger.info("Generated $file")
logger.info { "Generated $file" }
}
}
6 changes: 4 additions & 2 deletions service/codegen/src/main/kotlin/evaka/codegen/api/ApiFiles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import evaka.codegen.fileHeader
import fi.espoo.evaka.shared.auth.AuthenticatedUser
import fi.espoo.evaka.shared.utils.letIf
import fi.espoo.evaka.shared.utils.mapOfNotNullValues
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlin.reflect.KClass
import kotlin.reflect.jvm.jvmName
import kotlin.reflect.typeOf
import mu.KotlinLogging
import org.springframework.web.util.UriComponentsBuilder

private val logger = KotlinLogging.logger {}
Expand Down Expand Up @@ -181,7 +181,9 @@ fun generateApiTypes(
): String {
val conflicts = namedTypes.groupBy { it.name }.filter { it.value.size > 1 }
conflicts.forEach { (name, conflictingClasses) ->
logger.error("Multiple Kotlin classes map to $name: ${conflictingClasses.map { it.name }}")
logger.error {
"Multiple Kotlin classes map to $name: ${conflictingClasses.map { it.name }}"
}
}
if (conflicts.isNotEmpty()) {
error("${conflicts.size} classes are generated by more than one Kotlin class")
Expand Down
2 changes: 1 addition & 1 deletion service/evaka-bom/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies {
api("com.google.guava:guava:33.4.0-jre")
api("com.networknt:json-schema-validator:1.5.0")
api("com.zaxxer:HikariCP:6.2.1")
api("io.github.microutils:kotlin-logging-jvm:3.0.5")
api("io.github.oshai:kotlin-logging-jvm:7.0.3")
api("io.kotest:kotest-property:5.9.1")
api("io.mockk:mockk:1.13.13")
api("jakarta.annotation:jakarta.annotation-api:3.0.0")
Expand Down
2 changes: 1 addition & 1 deletion service/service-lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies {
testImplementation(kotlin("test-junit5"))

// Logging
implementation("io.github.microutils:kotlin-logging-jvm")
implementation("io.github.oshai:kotlin-logging-jvm")
implementation("net.logstash.logback:logstash-logback-encoder")

// Spring
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import jakarta.servlet.FilterChain
import jakarta.servlet.http.HttpFilter
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import mu.KotlinLogging
import io.github.oshai.kotlinlogging.KotlinLogging

// RFC6750 - The OAuth 2.0 Authorization Framework: Bearer Token Usage
// Authorization Request Header Field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,42 @@

package fi.espoo.voltti.logging.loggers

import mu.KLogger
import io.github.oshai.kotlinlogging.KLogger
import net.logstash.logback.argument.StructuredArguments

/**
* Extensions to allow adding meta-fields to app-misc logs without requiring StructuredArguments
* wrapper and an unnecessary dependency to net.logstash.logback.
*/
fun KLogger.trace(meta: Map<String, Any?>, m: () -> Any?) {
if (isTraceEnabled) trace(m.toStringSafe(), metaToLoggerArgs(meta))
fun KLogger.trace(meta: Map<String, Any?>, m: () -> Any?) = atTrace {
message = m.toStringSafe()
arguments = arrayOf(metaToLoggerArgs(meta))
}

fun KLogger.debug(meta: Map<String, Any?>, m: () -> Any?) {
if (isDebugEnabled) debug(m.toStringSafe(), metaToLoggerArgs(meta))
fun KLogger.debug(meta: Map<String, Any?>, m: () -> Any?) = atDebug {
message = m.toStringSafe()
arguments = arrayOf(metaToLoggerArgs(meta))
}

fun KLogger.info(meta: Map<String, Any?>, m: () -> Any?) {
if (isInfoEnabled) info(m.toStringSafe(), metaToLoggerArgs(meta))
fun KLogger.info(meta: Map<String, Any?>, m: () -> Any?) = atInfo {
message = m.toStringSafe()
arguments = arrayOf(metaToLoggerArgs(meta))
}

fun KLogger.warn(meta: Map<String, Any?>, m: () -> Any?) {
if (isWarnEnabled) warn(m.toStringSafe(), metaToLoggerArgs(meta))
fun KLogger.warn(meta: Map<String, Any?>, m: () -> Any?) = atWarn {
message = m.toStringSafe()
arguments = arrayOf(metaToLoggerArgs(meta))
}

fun KLogger.error(meta: Map<String, Any?>, m: () -> Any?) {
if (isErrorEnabled) error(m.toStringSafe(), metaToLoggerArgs(meta))
fun KLogger.error(meta: Map<String, Any?>, m: () -> Any?) = atError {
message = m.toStringSafe()
arguments = arrayOf(metaToLoggerArgs(meta))
}

fun KLogger.error(error: Any, meta: Map<String, Any?>, m: () -> Any?) {
if (isErrorEnabled) error(m.toStringSafe(), metaToLoggerArgs(meta), error)
fun KLogger.error(error: Throwable, meta: Map<String, Any?>, m: () -> Any?) = atError {
message = m.toStringSafe()
cause = error
arguments = arrayOf(metaToLoggerArgs(meta))
}

private fun metaToLoggerArgs(meta: Map<String, Any?>) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@

package fi.espoo.voltti.logging.loggers

import mu.KLogger
import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KMarkerFactory
import io.github.oshai.kotlinlogging.Marker
import net.logstash.logback.argument.StructuredArguments
import org.slf4j.Marker
import org.slf4j.MarkerFactory

val AUDIT_MARKER: Marker = MarkerFactory.getMarker("AUDIT_EVENT")
val AUDIT_MARKER: Marker = KMarkerFactory.getMarker("AUDIT_EVENT")

fun KLogger.audit(m: () -> Any?) {
audit(emptyMap(), m)
}
fun KLogger.audit(m: () -> Any?) = audit(emptyMap(), m)

fun KLogger.audit(t: Throwable, m: () -> Any?) {
if (isWarnEnabled) warn(AUDIT_MARKER, m.toStringSafe(), t)
}
fun KLogger.audit(t: Throwable, m: () -> Any?) = warn(t, AUDIT_MARKER, m)

fun KLogger.audit(args: Map<String, Any?>, m: () -> Any?) {
if (isWarnEnabled) warn(AUDIT_MARKER, m.toStringSafe(), StructuredArguments.entries(args))
}
fun KLogger.audit(args: Map<String, Any?>, m: () -> Any?) =
atWarn(AUDIT_MARKER) {
message = m.toStringSafe()
arguments = arrayOf(StructuredArguments.entries(args))
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@

package fi.espoo.voltti.logging.loggers

import mu.KLogger
import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KMarkerFactory
import net.logstash.logback.argument.StructuredArguments
import org.slf4j.MarkerFactory

private val auditMarker = MarkerFactory.getMarker("VTJ_EVENT")
private val auditMarker = KMarkerFactory.getMarker("VTJ_EVENT")

fun KLogger.auditVTJ(m: () -> Any?) {
auditVTJ(emptyMap(), m)
}
fun KLogger.auditVTJ(m: () -> Any?) = auditVTJ(emptyMap(), m)

fun KLogger.auditVTJ(args: Map<String, Any?>, m: () -> Any?) {
if (isWarnEnabled) warn(auditMarker, m.toStringSafe(), StructuredArguments.entries(args))
}
fun KLogger.auditVTJ(args: Map<String, Any?>, m: () -> Any?) =
atWarn(auditMarker) {
message = m.toStringSafe()
arguments = arrayOf(StructuredArguments.entries(args))
}

fun KLogger.auditVTJ(t: Throwable, m: () -> Any?) {
auditVTJ(emptyMap(), t, m)
}

fun KLogger.auditVTJ(args: Map<String, Any?>, t: Throwable, m: () -> Any?) {
if (isWarnEnabled) warn(auditMarker, m.toStringSafe(), t, StructuredArguments.entries(args))
}
fun KLogger.auditVTJ(args: Map<String, Any?>, t: Throwable, m: () -> Any?) =
atWarn(auditMarker) {
message = m.toStringSafe()
cause = t
arguments = arrayOf(StructuredArguments.entries(args))
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
package fi.espoo.voltti.logging.logback

import fi.espoo.voltti.logging.loggers.info
import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KMarkerFactory
import io.github.oshai.kotlinlogging.KotlinLogging
import java.util.UUID
import mu.KLogger
import mu.KMarkerFactory
import mu.KotlinLogging
import net.logstash.logback.argument.StructuredArguments
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
Expand All @@ -19,7 +19,10 @@ private val logger = KotlinLogging.logger {}
private val auditMarker = KMarkerFactory.getMarker("AUDIT_EVENT")

private fun KLogger.audit(msg: String, args: Map<String, Any>) =
info(auditMarker, msg, StructuredArguments.e(args))
atInfo(auditMarker) {
message = msg
arguments = arrayOf(StructuredArguments.e(args))
}

class AppenderTest {
private val meta = TestMeta.random()
Expand Down Expand Up @@ -55,12 +58,16 @@ class AppenderTest {

val event1 = DefaultEvent(userIdHash)
val exception = TestException()
logger.error(event1.message, StructuredArguments.e(mapOf("meta" to meta)), exception)
logger.atError {
message = event1.message
cause = exception
arguments = arrayOf(StructuredArguments.e(mapOf("meta" to meta)))
}
it.assertDefault().containsExactly(event1.tuple())
it.withLatestDefault { actual -> defaultErrorAssertions(actual, meta, exception) }

val event2 = DefaultEvent(userIdHash)
logger.info(event2.message)
logger.info { event2.message }
it.assertDefault().containsExactly(event1.tuple(), event2.tuple())
it.withLatestDefault { actual -> defaultInfoAssertions(actual) }

Expand All @@ -75,7 +82,11 @@ class AppenderTest {

val event1 = DefaultEvent(userIdHash)
val exception = TestExceptionSensitive()
logger.error(event1.message, StructuredArguments.e(mapOf("meta" to meta)), exception)
logger.atError {
message = event1.message
cause = exception
arguments = arrayOf(StructuredArguments.e(mapOf("meta" to meta)))
}
it.assertSanitized().containsExactly(event1.tuple())
it.withLatestSanitized { actual ->
defaultErrorAssertions(actual, meta, exception)
Expand All @@ -84,7 +95,7 @@ class AppenderTest {
}

val event2 = DefaultEvent(userIdHash)
logger.info(event2.message)
logger.info { event2.message }
it.assertSanitized().containsExactly(event1.tuple(), event2.tuple())
it.withLatestSanitized { actual -> defaultInfoAssertions(actual) }

Expand Down
Loading

0 comments on commit 0af3277

Please sign in to comment.