Skip to content

Commit

Permalink
Add logger(name: String)
Browse files Browse the repository at this point in the history
  • Loading branch information
leadrian committed Jul 29, 2023
1 parent 013bcef commit 80c312a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
3 changes: 1 addition & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ plugins {
`maven-publish`
signing
alias(libs.plugins.dokka)
alias(libs.plugins.git.versions)
alias(libs.plugins.git.version)
}

repositories {
Expand Down Expand Up @@ -52,7 +52,6 @@ version = gitVersion()
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
vendor.set(JvmVendorSpec.ADOPTOPENJDK)
}

withSourcesJar()
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
dokka = "1.8.20"
git-versions = "0.47.0"
git-version = "3.0.0"
junit = "5.10.0"
mockk = "1.13.5"
slf4j = "2.0.7"
Expand All @@ -14,4 +14,4 @@ slf4j-simple = { group = "org.slf4j", name = "slf4j-simple", version.ref = "slf4

[plugins]
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
git-versions = { id = "com.github.ben-manes.versions", version.ref = "git-versions" }
git-version = { id = "com.palantir.git-version", version = "3.0.0" }
7 changes: 6 additions & 1 deletion src/main/kotlin/ch/leadrian/slf4k/Slf4k.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ inline fun <reified T : Any> loggerFor(): Logger = LoggerFactory.getLogger(T::cl
fun Any.logger(): Logger {
val classToLog = when {
this::class.isCompanion -> javaClass.declaringClass ?: javaClass
else -> javaClass
else -> javaClass
}
return LoggerFactory.getLogger(classToLog)
}

/**
* @return a logger with the given name
*/
fun logger(name: String): Logger = LoggerFactory.getLogger(name)

/**
* Log a lazily computed message at TRACE level.
*/
Expand Down
13 changes: 12 additions & 1 deletion src/test/kotlin/ch/leadrian/slf4k/Slf4kTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

package ch.leadrian.slf4k

import io.mockk.*
import io.mockk.Runs
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.slf4j.Logger
Expand Down Expand Up @@ -527,5 +531,12 @@ internal class Slf4kTest {

assertEquals("ch.leadrian.slf4k.TestLoggers", logger.name)
}

@Test
fun `should create logger with given name`() {
val logger = logger("TestLogger")

assertEquals("TestLogger", logger.name)
}
}

0 comments on commit 80c312a

Please sign in to comment.