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

Addressing points raised in detektJvmMain task #203

Merged
merged 3 commits into from Dec 11, 2021

Conversation

severn-everett
Copy link
Contributor

Removed "exception as control flow" in KLoggerNameResolver.unwrapCompanionClass(); Consolidated MDC cleanup code in KotlinLoggingMDC

… control flow" in KLoggerNameResolver.unwrapCompanionClass(); Consolidated MDC cleanup code in KotlinLoggingMDC
@severn-everett
Copy link
Contributor Author

@oshai Is there something I need to fix in the PR so that the Greetings task doesn't produce the error message Error: Resource not accessible by integration?

@oshai
Copy link
Owner

oshai commented Dec 9, 2021

I think it's related to this issue, I will just ignore it.

@oshai
Copy link
Owner

oshai commented Dec 9, 2021

I will review it as soon as I have some time.

Copy link
Owner

@oshai oshai left a comment

Choose a reason for hiding this comment

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

in some cases I don't think those fixes improve readibility (commented on them). so please explain if you think there is a good reason to change

val previousMDC = if (restorePrevious) {
pairForMDC.map { (k, _) ->
k to MDC.get(k)
val cleanupCallbacks = pairForMDC.map { (mdcKey, _) ->
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think this is more readble

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code consolidates the cleanup logic in one place. Instead having an if-condition for restorePrevious both before and after the execution of the body payload, it's now only checked before the payload execution; afterwards, there's only one flow path: cleanupCallbacks executes whatever cleanup code it's been assigned.

@@ -91,7 +82,14 @@ public inline fun <T> withLoggingContext(
restorePrevious: Boolean = true,
body: () -> T
): T {
val previousMDC = map.mapValues { MDC.get(it.key) }
val cleanupCallbacks = map.map {
Copy link
Owner

Choose a reason for hiding this comment

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

same as above

}
}
return clazz
return clazz.enclosingClass?.let { enclosingClass ->
Copy link
Owner

Choose a reason for hiding this comment

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

I also don't see a good reason to change that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Searching through declaredFields instead of using getDeclaredField() has the advantage of not throwing NoSuchFieldException if the declared field cannot be found. If there's a Kotlin-implemented version of this functionality that doesn't use reflection and handles the "field not found" case more gracefully than the Java-implemented version, I'd be glad to use that, but in general, it's better to avoid using exceptions as control flow when possible.

@@ -558,7 +559,7 @@ internal class LocationAwareKLogger(override val underlyingLogger: LocationAware
override fun entry(vararg argArray: Any?) {
if (underlyingLogger.isTraceEnabled(ENTRY)) {
val tp = MessageFormatter.arrayFormat(buildMessagePattern(argArray.size), argArray)
underlyingLogger.log(ENTRY, fqcn, LocationAwareLogger.TRACE_INT, tp.message, null, null);
Copy link
Owner

Choose a reason for hiding this comment

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

nice!

@@ -182,4 +182,47 @@ class KotlinLoggingMDCTest {
assertNull(MDC.get("f"))
assertEquals("l", MDC.get("k"))
}

@Test
Copy link
Owner

Choose a reason for hiding this comment

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

thanks!

…nClass(); Consolidated testing code for mapping with logging context in KotlinLoggingMDCTest
@oshai oshai merged commit 981daea into oshai:master Dec 11, 2021
@oshai
Copy link
Owner

oshai commented Dec 11, 2021

Thanks!

@severn-everett severn-everett deleted the detekt_jvm_issues branch December 13, 2021 14:13
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.

None yet

2 participants