oshai / kotlin-logging

Lightweight Multiplatform logging framework for Kotlin. A convenient and performant logging facade.
Other
2.51k stars 110 forks source link

Why was KLogging deprecated? #399

Closed dbogdoll closed 1 month ago

dbogdoll commented 3 months ago

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?

github-actions[bot] commented 3 months ago

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

ewoelfel commented 2 months ago

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 {}
  }
}
alex-shinn commented 2 months ago

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.

oshai commented 2 months ago

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.