Skip to content

Instantly share code, notes, and snippets.

@nieldw
Created October 25, 2018 11:35
Show Gist options
  • Save nieldw/cf606d5b00337698afe12efa20c42740 to your computer and use it in GitHub Desktop.
Save nieldw/cf606d5b00337698afe12efa20c42740 to your computer and use it in GitHub Desktop.
Get a logger in Kotlin using the class name of a lambda
package com.example.nieldw
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import org.junit.jupiter.api.Test
fun logger(lambda: () -> Unit): Lazy<Logger> = lazy { LogManager.getLogger(getClassName(lambda.javaClass)) }
private fun <T : Any> getClassName(clazz: Class<T>): String = clazz.name.replace(Regex("""\$.*$"""), "")
class TopLevelLoggingTest {
val classLog by logger { }
@Test
fun `What is the javaClass?`() {
classLog.info("THIS IS IT")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment