Skip to content

Instantly share code, notes, and snippets.

@nieldw
Last active October 25, 2018 11:41
Show Gist options
  • Save nieldw/55bdca86c685914c2d26f75d7637a287 to your computer and use it in GitHub Desktop.
Save nieldw/55bdca86c685914c2d26f75d7637a287 to your computer and use it in GitHub Desktop.
Kotlin Top Level Function loggers
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("""\$.*$"""), "")
val topLog by logger { }
fun doLogging() {
topLog.info("THIS IS IT")
}
class TopLevelLoggingTest {
@Test
fun `What is the javaClass?`() {
doLogging()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment