Giter Club home page Giter Club logo

Comments (7)

oshai avatar oshai commented on July 28, 2024

I think this is a bug of MDCContext and kotlinx-coroutines. Can you please submit it there? Or do you want me to do that? here is the repo https://github.com/Kotlin/kotlinx.coroutines/blob/master/integration/kotlinx-coroutines-slf4j

from kotlin-logging.

casret avatar casret commented on July 28, 2024

I'm not really sure it's an upstream bug. I don't think they can do anything to influence that you are in a different thread before and after the withContext. We can either 'fix it there' by making some secondary constructors as fluent as withLoggingContext, or make withLoggingContext thread agnostic by making function local copies of the MDC (like in my example), but that approach is fundamentally incompatible with setting it on the thread level.

from kotlin-logging.

oshai avatar oshai commented on July 28, 2024

I do think it's an issue with upstream. withContext<T>(MDCContext(mdc)) means the context should be applied on all threads, in and out. At least that is how I understand it.

from kotlin-logging.

casret avatar casret commented on July 28, 2024

Sorry if I'm being unclear, the code block inside withContext runs correctly. The problem is that the values set by withLoggingContext don't get unset correctly. E.g. Thread A calls withLoggingContext and sets the foo key to bar. Inside withContext everything is great and foo returns bar no matter what thread it's running in. When withContext completes and hands control back to withLoggingContext it happens to be on thread B. Now withLoggingContext unsets foo, but on thread B. Thread A still has the bar value for foo when withLoggingContext exits, and will continue to log the wrong value until it happens to hit the same code again.

from kotlin-logging.

oshai avatar oshai commented on July 28, 2024

I think I understand now. Probably another method is a good idea. PR is welcome.

from kotlin-logging.

oshai avatar oshai commented on July 28, 2024

Closing it for now. PR is welcome.

from kotlin-logging.

ntfc avatar ntfc commented on July 28, 2024

I couldn't reproduce this based on @casret instructions, do you know if this issue is still applicable?

Here is the snippet I'm using to try and reproduce it which gives me the correct output:

@GetMapping("/{userId}")
suspend fun test(
    @PathVariable userId: String
) {
    withLoggingContext("userId" to userId) { // loggingContext must be callable before MDCContext
        withContext(MDCContext()) {
            log.info { "Start something for userId $userId" }
            delay(Random.nextLong(1_000, 5_000))
            log.info { "Finished for userId $userId" }
        }
    }

    log.info { "Which userId am I? $userId" }
}

With the following JSON formatted output:

{
  "@timestamp": "2021-05-07T16:57:33.484+02:00",
  "@version": "1",
  "message": "Start something for userId 42",
  "logger_name": "me.ntfc.TestApiResource",
  "thread_name": "http-nio-8090-exec-2",
  "level": "INFO",
  "level_value": 20000,
  "userId": "42"
}
{
  "@timestamp": "2021-05-07T16:57:38.139+02:00",
  "@version": "1",
  "message": "Finished for userId 42",
  "logger_name": "me.ntfc.TestApiResource",
  "thread_name": "kotlinx.coroutines.DefaultExecutor",
  "level": "INFO",
  "level_value": 20000,
  "userId": "42"
}
{
  "@timestamp": "2021-05-07T16:57:38.142+02:00",
  "@version": "1",
  "message": "Which userId am I? 42",
  "logger_name": "me.ntfc.TestApiResource",
  "thread_name": "kotlinx.coroutines.DefaultExecutor",
  "level": "INFO",
  "level_value": 20000
}

from kotlin-logging.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.