Powered by SLF4J, fully inlined, and say goodbye to lambda object creation π²
- Java 8+
- Kotlin 1.5.0+
- Fully inlined SLF4J logger
- lazily create log message without creating lambda object (because everything is inlined π)
- only showing lazy logging methods on your IDE (e.g. showing
logger.info {}
on your auto-complete hint instead oflogger.info()
)
Check JitPack to see how to import
Simply add
private val logger = KInlineLogging.logger()
to anywhere you want and enjoy logging π
For example:
private val logger = KInlineLogging.logger() // an instance of KInlineLogger, which is a value class of SLF4J Logger
class MainApp {
fun start() {
logger.info { "an info msg" }
val exception = SomeException()
logger.debug(exception) { "a debug msg with exception $exception" }
}
}
Under the hook, this should be compiled as:
private val logger: Logger = LoggerFactory.getLogger(
KInlineLoggerNameResolver.fixName(MethodHandles.lookup().lookupClass())
) // A SLF4J Logger, not the KInlineLogger π―
class MainApp {
fun start() {
if (logger.isInfoEnabled()){
logger.info("an info msg") // no lambda object created π
}
val exception = SomeException()
if (logger.isDebugEnabled()){
// notice that this string creation will not happen if the debug level is not enabled
logger.debug("a debug msg with exception $exception", exception)
}
}
}
A: Sorry, I am not supporting it, but you can just grab the only two source files in this library and modify it to suit your needs.