Skip to content

Commit 24ad8d0

Browse files
committed
Use method instead of property for LogField.keyForLoggingContext
We want to encourage calling this just once for a scope, storing it in a local variable, to reduce calls to the abstract method.
1 parent a9a6d04 commit 24ad8d0

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

src/commonMain/kotlin/dev/hermannm/devlog/LogField.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public sealed class LogField(
8282
* context to identify the value as raw JSON (so we can write the JSON unescaped in
8383
* `LoggingContextJsonFieldWriter`).
8484
*/
85-
internal abstract val keyForLoggingContext: String
85+
internal abstract fun getKeyForLoggingContext(): String
8686

8787
/**
8888
* Returns false if the field should not be included in the log (used by
@@ -102,21 +102,22 @@ public sealed class LogField(
102102

103103
@PublishedApi
104104
internal open class StringLogField(key: String, value: String) : LogField(key, value) {
105-
final override val keyForLoggingContext: String
106-
get() = key
105+
final override fun getKeyForLoggingContext(): String = key
107106
}
108107

109108
@PublishedApi
110109
internal open class JsonLogField(
111110
key: String,
112111
value: String,
113-
final override val keyForLoggingContext: String =
112+
private val keyForLoggingContext: String =
114113
if (ADD_JSON_SUFFIX_TO_LOGGING_CONTEXT_KEYS) {
115114
key + LOGGING_CONTEXT_JSON_KEY_SUFFIX
116115
} else {
117116
key
118117
}
119118
) : LogField(key, value) {
119+
final override fun getKeyForLoggingContext(): String = keyForLoggingContext
120+
120121
@PublishedApi
121122
internal companion object {
122123
/**

src/commonTest/kotlin/dev/hermannm/devlog/testutils/LoggingContextUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal infix fun LoggingContext.shouldContainExactly(map: Map<String, String>)
1212
contextFields.size shouldBe map.size
1313
for ((key, value) in map) {
1414
withClue({ "key='${key}', value='${value}'" }) {
15-
val field = contextFields.find { field -> field.keyForLoggingContext == key }
15+
val field = contextFields.find { field -> field.getKeyForLoggingContext() == key }
1616
field.shouldNotBeNull()
1717
field.value shouldBe value
1818
}

src/jvmMain/kotlin/dev/hermannm/devlog/LoggingContext.jvm.kt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ internal actual object LoggingContext {
2727

2828
for (index in fields.indices) {
2929
val field = fields[index]
30+
val keyForLoggingContext = field.getKeyForLoggingContext()
3031

3132
// Skip duplicate keys in the field array
3233
if (isDuplicateField(field, index, fields)) {
@@ -51,30 +52,30 @@ internal actual object LoggingContext {
5152
* here. The previous field will then be restored by [removeFields] after the context
5253
* exits.
5354
*/
54-
if (field.key != field.keyForLoggingContext) {
55+
if (field.key != keyForLoggingContext) {
5556
MDC.remove(field.key)
5657
}
5758
}
5859
}
5960

6061
/**
61-
* [JsonLogField] adds a suffix to [LogField.keyForLoggingContext], i.e. it will be different
62-
* from [LogField.key]. In this case, we want to check existing context field values for both
63-
* [LogField.key] _and_ [LogField.keyForLoggingContext].
62+
* [JsonLogField] adds a suffix to `keyForLoggingContext`, i.e. it will be different from
63+
* [LogField.key]. In this case, we want to check existing context field values for both `key`
64+
* _and_ `keyForLoggingContext`.
6465
*/
65-
if (field.key != field.keyForLoggingContext && existingValue == null) {
66-
existingValue = MDC.get(field.keyForLoggingContext)
66+
if (field.key != keyForLoggingContext && existingValue == null) {
67+
existingValue = MDC.get(keyForLoggingContext)
6768
when (existingValue) {
6869
null -> {}
6970
field.value -> continue
7071
else -> {
7172
overwrittenFields =
72-
overwrittenFields.set(index, field.keyForLoggingContext, existingValue, fields.size)
73+
overwrittenFields.set(index, keyForLoggingContext, existingValue, fields.size)
7374
}
7475
}
7576
}
7677

77-
MDC.put(field.keyForLoggingContext, field.value)
78+
MDC.put(keyForLoggingContext, field.value)
7879
}
7980

8081
return overwrittenFields
@@ -92,6 +93,7 @@ internal actual object LoggingContext {
9293
) {
9394
for (index in fields.indices) {
9495
val field = fields[index]
96+
val keyForLoggingContext = field.getKeyForLoggingContext()
9597

9698
// Skip duplicate keys, like we do in addFields
9799
if (isDuplicateField(field, index, fields)) {
@@ -106,12 +108,12 @@ internal actual object LoggingContext {
106108
* to call `MDC.remove` below (these may not always match for [JsonLogField] - see docstring
107109
* over `MDC.remove` in [addFields]).
108110
*/
109-
if (overwrittenKey == field.keyForLoggingContext) {
111+
if (overwrittenKey == keyForLoggingContext) {
110112
continue
111113
}
112114
}
113115

114-
MDC.remove(field.keyForLoggingContext)
116+
MDC.remove(keyForLoggingContext)
115117
}
116118
}
117119

0 commit comments

Comments
 (0)