Last active
February 13, 2023 07:59
-
-
Save dmikurube/59f8e7d6fa32541067b252d612c3458b to your computer and use it in GitHub Desktop.
Exception handling with logging in Kotlin coroutines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plugins { | |
id "org.jetbrains.kotlin.jvm" version "1.7.22" | |
id "application" | |
} | |
sourceSets { | |
main { | |
kotlin { | |
srcDir "." | |
include "*.kt" | |
} | |
resources { | |
srcDir "." | |
include "*.xml" | |
} | |
} | |
} | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4") | |
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-slf4j:1.6.4") | |
implementation("io.github.microutils:kotlin-logging:3.0.4") | |
implementation("ch.qos.logback:logback-classic:1.3.5") | |
} | |
application { | |
mainClass = "CoroutineExceptionHandlerLoggingKt" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.CoroutineExceptionHandler | |
import kotlinx.coroutines.CoroutineScope | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.Job | |
import kotlinx.coroutines.SupervisorJob | |
import kotlinx.coroutines.delay | |
import kotlinx.coroutines.launch | |
import kotlinx.coroutines.runBlocking | |
import kotlinx.coroutines.slf4j.MDCContext | |
import mu.KotlinLogging | |
import org.slf4j.MDC | |
private val logger = KotlinLogging.logger {} | |
fun main() = runBlocking { | |
var handler: CoroutineExceptionHandler = CoroutineExceptionHandler { context, exception -> | |
logger.error(exception) { "Exception-logging" } | |
} | |
val scope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO + handler) | |
val jobs: MutableList<Job> = mutableListOf() | |
for (i in 0..9) { | |
MDC.put("foo", i.toString()) | |
jobs.add(scope.launch(MDCContext()) { | |
if (i < 5) { | |
delay(100) | |
} else { | |
delay(200) | |
} | |
println("Print[${i}]") | |
throw RuntimeException("RuntimeException[${i}]") | |
}) | |
} | |
for (job in jobs) { | |
job.join() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE logback> | |
<configuration> | |
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | |
<encoder> | |
<pattern>%n[%X{foo}] %m%n</pattern> | |
</encoder> | |
</appender> | |
<root> | |
<appender-ref ref="STDOUT" /> | |
</root> | |
</configuration> |
Author
dmikurube
commented
Feb 13, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment