Created
October 25, 2018 11:35
-
-
Save nieldw/cf606d5b00337698afe12efa20c42740 to your computer and use it in GitHub Desktop.
Get a logger in Kotlin using the class name of a lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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