Skip to content

Commit 5ca1542

Browse files
committed
Use 'contextFields' consistently for logging context field variables
1 parent 2b905e6 commit 5ca1542

File tree

5 files changed

+42
-39
lines changed

5 files changed

+42
-39
lines changed

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -184,30 +184,30 @@ public open class ExceptionWithLoggingContext : RuntimeException {
184184
}
185185
}
186186

187-
val loggingContext = this.loggingContext
188-
if (loggingContext != null) {
189-
logBuilder.addFields(loggingContext)
187+
val contextFields = this.contextFields
188+
if (contextFields != null) {
189+
logBuilder.addFields(contextFields)
190190
}
191191
}
192192

193193
/**
194194
* Used by [withLoggingContext] to attach logging context if this exception escapes a context
195195
* scope.
196196
*/
197-
@kotlin.concurrent.Volatile private var loggingContext: Array<out LogField>? = null
197+
@kotlin.concurrent.Volatile private var contextFields: Array<out LogField>? = null
198198

199-
internal fun addLoggingContext(logFields: Array<out LogField>) {
200-
val existingContext = this.loggingContext
201-
if (existingContext == null) {
202-
this.loggingContext = logFields
199+
internal fun addLoggingContext(newContextFields: Array<out LogField>) {
200+
val existingContextFields = this.contextFields
201+
if (existingContextFields == null) {
202+
this.contextFields = newContextFields
203203
return
204204
}
205205

206206
// We need to cast to `Array<LogField>` in order to use the `+` operator here (which we want to
207207
// use, since it uses optimized `System.arraycopy` on JVM).
208208
// We can safely cast from `Array<out LogField>` to `Array<LogField>`, since `LogField` has no
209209
// subclasses, so this doesn't break covariance.
210-
this.loggingContext = (existingContext as Array<LogField>) + logFields
210+
this.contextFields = (existingContextFields as Array<LogField>) + newContextFields
211211
}
212212
}
213213

@@ -323,9 +323,9 @@ public interface HasLoggingContext {
323323
* an exception.
324324
*/
325325
internal expect class LoggingContextProvider : RuntimeException {
326-
constructor(loggingContext: Array<out LogField>)
326+
constructor(contextFields: Array<out LogField>)
327327

328-
fun addLoggingContext(logFields: Array<out LogField>)
328+
fun addLoggingContext(newContextFields: Array<out LogField>)
329329

330330
fun addFieldsToLog(logBuilder: LogBuilder)
331331
}
@@ -341,7 +341,7 @@ internal expect class LoggingContextProvider : RuntimeException {
341341
* logging context fields in the exception message, so we don't lose context.
342342
*/
343343
internal fun getLoggingContextProviderMessage(
344-
loggingContext: Array<out LogField>,
344+
contextFields: Array<out LogField>,
345345
fieldsAddedToLog: Boolean,
346346
): String {
347347
if (fieldsAddedToLog) {
@@ -353,24 +353,24 @@ internal fun getLoggingContextProviderMessage(
353353
// Pre-calculate capacity, so that StringBuilder only has to allocate once
354354
val capacity =
355355
messagePrefix.length +
356-
loggingContext.sumOf { logField ->
356+
contextFields.sumOf { field ->
357357
// +5 chars for "\n\t\t" before each field and ": " between key and value
358-
logField.key.length + logField.value.length + 5
358+
field.key.length + field.value.length + 5
359359
}
360360

361361
val message = StringBuilder(capacity)
362362
message.append(messagePrefix)
363363

364-
for (logField in loggingContext) {
364+
for (field in contextFields) {
365365
// Use double tabs here, since we want the fields to be indented, and suppressed exceptions
366366
// (which we use for `LoggingContextProvider`) are already indented by one tab
367367
message.append('\n')
368368
message.append('\t')
369369
message.append('\t')
370-
message.append(logField.key)
370+
message.append(field.key)
371371
message.append(':')
372372
message.append(' ')
373-
message.append(logField.value)
373+
message.append(field.value)
374374
}
375375

376376
return message.toString()

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ public expect fun getCopyOfLoggingContext(): LoggingContext
352352
* [withLoggingContext] overloads in order to copy logging context between threads.
353353
*/
354354
public expect class LoggingContext {
355-
internal fun toLogFields(): Array<out LogField>?
355+
internal fun getFields(): Array<out LogField>?
356356
}
357357

358358
/** Static field for the empty logging context, to avoid redundant re-instantiations. */
@@ -370,8 +370,11 @@ internal expect fun addExistingContextFieldsToLoggingContext(existingContext: Lo
370370
internal expect fun removeExistingContextFieldsFromLoggingContext(existingContext: LoggingContext)
371371

372372
@PublishedApi
373-
internal fun addLoggingContextToException(exception: Throwable, logFields: Array<out LogField>) {
374-
if (logFields.isEmpty()) {
373+
internal fun addLoggingContextToException(
374+
exception: Throwable,
375+
contextFields: Array<out LogField>,
376+
) {
377+
if (contextFields.isEmpty()) {
375378
return
376379
}
377380

@@ -383,11 +386,11 @@ internal fun addLoggingContextToException(exception: Throwable, logFields: Array
383386
traverseExceptionTree(root = exception) { exception ->
384387
when (exception) {
385388
is ExceptionWithLoggingContext -> {
386-
exception.addLoggingContext(logFields)
389+
exception.addLoggingContext(contextFields)
387390
return
388391
}
389392
is LoggingContextProvider -> {
390-
exception.addLoggingContext(logFields)
393+
exception.addLoggingContext(contextFields)
391394
return
392395
}
393396
}
@@ -397,17 +400,17 @@ internal fun addLoggingContextToException(exception: Throwable, logFields: Array
397400
* If there were no eligible exception to add the context to, we add a suppressed
398401
* [LoggingContextProvider] - see its docstring for more on how this mechanism works.
399402
*/
400-
exception.addSuppressed(LoggingContextProvider(logFields))
403+
exception.addSuppressed(LoggingContextProvider(contextFields))
401404
}
402405

403406
@PublishedApi
404407
internal fun addExistingLoggingContextToException(
405408
exception: Throwable,
406409
existingContext: LoggingContext
407410
) {
408-
val logFields = existingContext.toLogFields()
409-
if (logFields != null) {
410-
addLoggingContextToException(exception, logFields)
411+
val contextFields = existingContext.getFields()
412+
if (contextFields != null) {
413+
addLoggingContextToException(exception, contextFields)
411414
}
412415
}
413416

src/commonTest/kotlin/dev/hermannm/devlog/LoggingContextTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ internal class LoggingContextTest {
538538
fun `LoggingContextProvider has expected exception message and empty stack trace`() {
539539
val loggingContextProvider =
540540
LoggingContextProvider(
541-
loggingContext =
541+
contextFields =
542542
arrayOf(
543543
field("key1", "value"),
544544
field("key2", Event(id = 1, type = EventType.ORDER_PLACED)),

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package dev.hermannm.devlog
55
// See docstring on the `expect` declaration in `ExceptionWithLoggingContext.kt` under `commonMain`.
66
internal actual class LoggingContextProvider
77
actual constructor(
8-
@kotlin.concurrent.Volatile private var loggingContext: Array<out LogField>,
8+
@kotlin.concurrent.Volatile private var contextFields: Array<out LogField>,
99
) : RuntimeException() {
1010
/**
1111
* We set this to true when [addFieldsToLog] is called.
@@ -14,21 +14,21 @@ actual constructor(
1414
*/
1515
@kotlin.concurrent.Volatile private var fieldsAddedToLog = false
1616

17-
actual fun addLoggingContext(logFields: Array<out LogField>) {
17+
actual fun addLoggingContext(newContextFields: Array<out LogField>) {
1818
// We need to cast to `Array<LogField>` in order to use the `+` operator here (which we want to
1919
// use, since it uses optimized `System.arraycopy` on JVM).
2020
// We can safely cast from `Array<out LogField>` to `Array<LogField>`, since `LogField` has no
2121
// subclasses, so this doesn't break covariance.
22-
this.loggingContext = (this.loggingContext as Array<LogField>) + logFields
22+
this.contextFields = (this.contextFields as Array<LogField>) + newContextFields
2323
}
2424

2525
actual fun addFieldsToLog(logBuilder: LogBuilder) {
26-
logBuilder.addFields(loggingContext)
26+
logBuilder.addFields(contextFields)
2727
this.fieldsAddedToLog = true
2828
}
2929

3030
override val message: String?
31-
get() = getLoggingContextProviderMessage(loggingContext, fieldsAddedToLog)
31+
get() = getLoggingContextProviderMessage(contextFields, fieldsAddedToLog)
3232

3333
override fun fillInStackTrace(): Throwable {
3434
return this

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,31 @@ internal constructor(
2929
return map.isNullOrEmpty()
3030
}
3131

32-
internal actual fun toLogFields(): Array<out LogField>? {
33-
if (map == null) {
32+
internal actual fun getFields(): Array<out LogField>? {
33+
if (map.isNullOrEmpty()) {
3434
return null
3535
}
3636

37-
val logFields: Array<LogField?> = arrayOfNulls(map.size)
37+
val contextFields: Array<LogField?> = arrayOfNulls(map.size)
3838
var index = 0
3939
for ((key, value) in map) {
40-
val logField: LogField =
40+
val field: LogField =
4141
if (value == null) {
42-
LogField(key, value = JSON_NULL_VALUE, isJson = true)
42+
LogField(key, JSON_NULL_VALUE, isJson = true)
4343
} else {
4444
val isJson = state.isJsonField(key, value)
4545
LogField(key, value, isJson)
4646
}
4747

48-
logFields[index] = logField
48+
contextFields[index] = field
4949

5050
index++
5151
}
5252

5353
// Safe to cast here: We initialize `logFields` with `map.size`, and then add a log field for
5454
// every entry in the map, so all elements in the array will be initialized
5555
@Suppress("UNCHECKED_CAST")
56-
return logFields as Array<LogField>
56+
return contextFields as Array<LogField>
5757
}
5858
}
5959

0 commit comments

Comments
 (0)