Created
May 19, 2022 19:54
-
-
Save matthewadams/ba8fff9fd04c3e05aa9b29952a779a0f to your computer and use it in GitHub Desktop.
Convenient logback json support using Kotlin extensions
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
// NOTE: inspired by https://www.innoq.com/en/blog/structured-logging/#structuredarguments | |
import net.logstash.logback.argument.StructuredArguments.kv | |
import org.slf4j.Logger | |
const val DEFAULT_KEY = "ctx" | |
internal fun o(ctx: Any? = null) = kv( | |
DEFAULT_KEY, | |
object { | |
val `class` = if (ctx == null) "(null)" else ctx::class.java.canonicalName | |
val `object` = ctx | |
} | |
) | |
fun Logger.traceJson(format: String, ctx: Any? = null) { | |
this.trace(format, o(ctx)) | |
} | |
fun Logger.debugJson(format: String, ctx: Any? = null) { | |
this.debug(format, o(ctx)) | |
} | |
fun Logger.infoJson(format: String, ctx: Any? = null) { | |
this.info(format, o(ctx)) | |
} | |
fun Logger.warnJson(format: String, ctx: Any? = null) { | |
this.warn(format, o(ctx)) | |
} | |
fun Logger.errorJson(format: String, ctx: Any? = null) { | |
this.error(format, o(ctx)) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment