Replies: 1 comment
-
|
@mcmacker4, you're at the right place to raise this concern. @rocketraman is the only active maintainer, maybe he can chime in. In its current state, Log4j Kotlin is pretty under-staffed. I tried retiring it, but failed. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
As a preface, i will say that i am writing this here but I am not entirely sure that this is the appropriate forum. It looks to me as if
log4j-api-kotlinis an independent project based on the fact that it is hosted in a different github repo and it is not included inlog4j-bom. If this is not the correct forum, I would appreciate being pointed in the right direction. I didn't want to open an actual github issue in the repo before even discussing this somewhere, but that repo doesn't have discussions enabled, so I will use this one as fallback.While i was experimenting with reactive spring, i added log4j's kotlin api, which is supposed to use kotlin's super cool powers to make
log4jmore kotlin idiomatic by moving lambdas to the last argument and promoting string templates instead of log4j's template arguments.Another of kotlin's powers is function call inlining. What i noticed, though, is that none of the lambda based functions in
KotlinLoggerare inline.Was this discussed at any point? I cannot find any discussion or issues related to this topic.
Designing KotlinLogger's lambda based functions to be inline would allow things like suspending in the middle of the lambda.
I noticed this quirk when i wanted to log the current coroutine context. The function
currentCoroutineContext()fromkotlinx-coroutinesis a suspending function, but the following code will not compile:The problem here is that, since the lambda passed to the
info()method is neithersuspendnor inlined, you cannot call a suspending function inside that lambda. Marking the lambda assuspendwould not allow use in regular non-suspendcode.This is because the implementation delegates to
logIfEnabled, turning the lambda reference into aorg.apache.logging.log4j.util.Supplier. On the other hand, if theinfo()method was redesigned to allow inlining, the error would disappear. See this simple example:With this change, the method call would be translated into the if statement in the method body, and the call to the lambda would also be inlined.
This function:
Compiles into this code (decompiled with fernflower, slightly cleaned up for readability)
As you can see, the call to
expensiveOperation()happens directly inmain, but only if theinfolevel is enabled.What this means is that now, if the call site is a suspend function, you can call a suspend function in the lambda and, after compilation, it would be called directly in the suspend function, which is legal, so the first code block in this post (the one that failed to compile when calling
currentCoroutineContext()) would compile and work just fine.Beta Was this translation helpful? Give feedback.
All reactions