Skip to content

Issue #499 - improve Kotlin/JS logger #500

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

scrat98
Copy link
Contributor

@scrat98 scrat98 commented Apr 17, 2025

Closes #499

@oshai
Copy link
Owner

oshai commented Apr 20, 2025

The problem with JS IIRC was that there is nodejs and browser that works completely different in the sense of obtaining logger name (maybe there was another use case I don't remember right now).

So the question is how do we test it to make sure we improved both cases.

Also if you can provide more details on the change it will be helpful to review.

@oshai
Copy link
Owner

oshai commented Apr 20, 2025

I am referring: #279 (comment)

@scrat98
Copy link
Contributor Author

scrat98 commented Apr 23, 2025

@oshai

So the question is how do we test it to make sure we improved both cases.

Every use case is covered by test for both JS and WASM. Please, have a look at the SimpleJsTest and SimpleWasmTest headers, there you'll find all use cases that were tested.

val topLevelNamedLogger = KotlinLogging.logger("topLevelNamedLogger")
val topLevelLambdaLogger = KotlinLogging.logger {}

class MyClass {
  val classNamedLogger = KotlinLogging.logger("MyClass")
  val classLambdaLogger = KotlinLogging.logger {}

  // check with non default "Companion" name also
  companion object MyCompanion {
    val companionNamedLogger = KotlinLogging.logger("MyClassCompanion")
    val companionLambdaLogger = KotlinLogging.logger {}
  }
}

You may run these tests with current (master) implementation and find out the tests failure.

Also if you can provide more details on the change it will be helpful to review.

Logger name for both JS and WASM is derived via stack trace analysis to cover all use cases. Currently this is the only way to properly obtain class name for all use cases. Analysis looks like:

  1. Find logger invocation line (LOGGER_FUNCTION_NAME). For JS @JSName was added
  2. Try to resolve it as a top level property (TOP_LEVEL_INIT_PROPERTIES_REGEX)
  3. Try to resolve it as class level property (CLASS_LEVEL_INIT_PROPERTIES_REGEX). Also taking in the consideration Companion object (COMPANION_GET_INSTANCE_SUFFIX)
  4. Fallback to the DEFAULT_LOGGER_NAME

Also you can find description here #499

logger.info { "info msg" }
assertEquals("INFO: [SimpleJsTest] info msg", appender.lastMessage)
assertEquals("INFO: [${logger.name}] info msg", appender.lastMessage)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you change this to an explicit string param with logger name, so it will be clear what was the name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's checking via checkLoggerName(logger: KLogger, expected: String)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oshai Hi, could we move this PR forward?

@oshai
Copy link
Owner

oshai commented Jun 29, 2025

I think the PR is too big at the moment.
I prefer to do smaller changes iteratively (maybe without initial breaking changes).
I am not convinced this will work for both ir/legacy and browser/node (even without talking about wasm).

@scrat98
Copy link
Contributor Author

scrat98 commented Jun 29, 2025

@oshai

I think the PR is too big at the moment.
I prefer to do smaller changes iteratively

Do you have any suggestions on how to make it smaller? I couldn't find any obvious options myself. Most of the code lines are tests.

maybe without initial breaking changes

As for the breaking changes, I believe it should be acceptable, since users would need to update the workaround syntax

private val logger by KotlinLogging.logger()

to the idiomatic version

private val logger = KotlinLogging.logger {} 

I am not convinced this will work for both ir/legacy and browser/node (even without talking about wasm).

As mentioned earlier, both the browser and nodejs use cases are covered by the tests. If you don't mind to point out any parts of the code that aren't covered by tests, I’d really appreciate it.

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

Successfully merging this pull request may close these issues.

Improve Kotlin/Js logger
2 participants