Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why was KLogging deprecated? #399

Closed
dbogdoll opened this issue Feb 7, 2024 · 4 comments
Closed

Why was KLogging deprecated? #399

dbogdoll opened this issue Feb 7, 2024 · 4 comments

Comments

@dbogdoll
Copy link

dbogdoll commented Feb 7, 2024

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?

Copy link

github-actions bot commented Feb 7, 2024

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

@ewoelfel
Copy link
Contributor

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
Copy link

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
Copy link
Owner

oshai commented Mar 26, 2024

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 oshai closed this as completed Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants