kotlin-logging
kotlin-logging copied to clipboard
Why was KLogging deprecated?
I see that the following usage of KLogging is now deprecated:
class MyClass {
companion object: KLogging()
init {
logger.info("Example")
}
}
If this usage is deprecated, with what idiom should I replace it?
Thank you for reporting an issue. See the wiki for documentation and slack for questions.
you can do the following:
private val logger = KotlinLogging.logger {}
class MyClass {
init {
logger.info{ "Example" }
}
}
or if you like to use a companion object:
class MyClass {
init {
logger.info{ "Example" }
}
companion object {
private val logger = KotlinLogging.logger {}
}
}
This begs the question why was the raw string usage deprecated?
The lambda is an optimization only if the log is ultimately not omitted (and the log string contains interpolation). Otherwise it's actually an (admittedly small) pessimization, since you need the extra bytecode and memory usage for the lambda, and extra CPU for invocation.
In practice the log level is fixed in production, and we would never want to suppress info
or higher logs anyway. So in our codebase we tend to use lambdas for expensive debug logs and strings for everything else. Forcing everything to be a lambda is just wasteful.
This begs the question why was the raw string usage deprecated?
The main motivation was to make the api clearer. With the old api methods parameters matrix became too complex (raw string also requires parameters, throwable, markers etc'). That's why most frameworks are moving toward fluent api. I tried to make the api "kotlin native" and clearer for users. I agreed it's opinionated compared to previous version.