Comments (7)
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.
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.
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.
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.
I think I understand now. Probably another method is a good idea. PR is welcome.
from kotlin-logging.
Closing it for now. PR is welcome.
from kotlin-logging.
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)
- Payload not available to JS HOT 7
- underlyingLogger got a lot more complex HOT 5
- sflj jdk backend does not recognize class name HOT 14
- Gradle logging HOT 2
- Problems regarding Android HOT 3
- Problem using LogstashMarker HOT 15
- Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory HOT 2
- Declaring logger outside the class vs. inside HOT 8
- Possible to add a module-info.java for improved JPMS support? HOT 3
- setting logging level per KLogger instance in run-time HOT 4
- Built JAR Errors - SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" HOT 3
- Darwin doesn't log stacktraces HOT 2
- Publish Dokka HTML docs on project site HOT 2
- Why was KLogging deprecated? HOT 4
- KLoggingEventBuilder accept nullable values in payload HOT 4
- IllegalArgumentException when message includes {} HOT 5
- Android native targets missing. HOT 3
- Wrong class name printed when using LoggingEventBuilder with payload HOT 10
- Undo workaround for Gradle issue #11412 HOT 1
- Add timestamp to the KLoggingEvent HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlin-logging.