MdcDecorator.kt

package com.example.templateproject.client

import org.slf4j.MDC
import org.springframework.core.task.TaskDecorator

internal class MdcDecorator : TaskDecorator {
    override fun decorate(runnable: Runnable): Runnable {
        val contextMap = MDC.getCopyOfContextMap()
        return Runnable {
            try {
                contextMap?.let { MDC.setContextMap(it) }
                runnable.run()
            } finally {
                MDC.clear()
            }
        }
    }
}