Skip to content

v1.4.0

Choose a tag to compare

@shivathapaa shivathapaa released this 19 Apr 03:55
· 16 commits to main since this release

What's New

logger-coroutines module

New optional module for LogContext propagation across coroutine suspension and thread switches.

On JVM/Android, the core logger uses ThreadLocal. When a coroutine resumes on another thread (Dispatchers.IO, Dispatchers.Default), context is lost. This module fixes it.

Add dependency:

// build.gradle.kts (commonMain)
commonMain.dependencies {
    implementation("io.github.shivathapaa:logger:1.4.0")
    implementation("io.github.shivathapaa:logger-coroutines:1.4.0") // optional
}

withLogContext

Coroutine-safe context scoping:

withLogContext(LogContext(mapOf("requestId" to "req-123", "userId" to 42))) {
    logger.info { "Before suspension" }

    withContext(Dispatchers.IO) {
        delay(100)
        logger.debug { "Fetching data" }
    }

    logger.info { "Request complete" }
}

Platform behavior

Platform Mechanism
JVM / Android LogContextElement implements ThreadContextElement and restores context on thread switch
iOS / macOS / Apple Single-threaded, uses direct install and finally restore
JS / WasmJS / Linux / MinGW Same as Apple

LogContextElement

Attach context to a scope:

val scope = CoroutineScope(
    Dispatchers.IO + LogContextElement(LogContext(mapOf("service" to "payment-api")))
)

withSuspendingContext

Basic suspend support in core for single-thread execution:

LogContextHolder.withSuspendingContext(ctx) {
    delay(100)
    logger.info { "Context present" }
}

Note: For Dispatchers.IO or Dispatchers.Default on JVM/Android, use withLogContext.

Other changes

  • Tests: added coverage for context propagation and isolation

  • Sample: updated apps with coroutine context demos

  • Deps:

    • Gradle 9.1.0 to 9.3.1
    • kotlinx-coroutines-core 1.10.2

Upgrade guide

No breaking changes. logger API unchanged. logger-coroutines is optional.