kotlin-logging icon indicating copy to clipboard operation
kotlin-logging copied to clipboard

Why was KLogging deprecated?

Open dbogdoll opened this issue 1 year ago • 1 comments

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?

dbogdoll avatar Feb 07 '24 15:02 dbogdoll

Thank you for reporting an issue. See the wiki for documentation and slack for questions.

github-actions[bot] avatar Feb 07 '24 15:02 github-actions[bot]

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 {}
  }
}

ewoelfel avatar Mar 22 '24 15:03 ewoelfel

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.

alex-shinn avatar Mar 26 '24 08:03 alex-shinn

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.

oshai avatar Mar 26 '24 10:03 oshai