Skip to content

Can't get extra fields in json output without relying on deprecated methods #457

@sigmanil

Description

@sigmanil

Hi, and thank you for maintaining an excellent library!

We have unfortunately run into a little problem:

We noticed that the support for writing log statements on the form log.error("Cake", StructuredArguments.keyValue("caketype", "chocolate")) (with net.logstash.logback.argument.StructuredArguments) has been deprecated. We use that style to include extra fields for json logging.

The replace-with in the deprecation creates a wholly different result. After some searching, we noticed the "atError" etc function that take a payload, and hoped they could be used to replace the old behaviour.

log.atError {
   message = "Cake"
   payload = buildMap {
      put("caketype", "chocolate")
    }
}

But while that does include the caketype/chocolate pair in the output for ordinary logging, it disappears when we use Json logging.

For a fairly minimal example, see the below files and the comments in main. The behaviour of atError seems to be opposite of the behavior of the old method, in that the old one doesn't include the extra field in "ordinary" logging, while the new one does - and vice versa when it comes to json logging.

Have I misunderstood what the payload argument of atInfo, atWarn etc functions are supposed to be used for? If so, how do we reproduce the old behaviour for including extra fields in json output without leaning on the deprecated method? (Or is this a bug?)

Thanks in advance for any hints or ideas, we're a bit stuck here. :-)

Main.kt

import io.github.oshai.kotlinlogging.KotlinLogging
import net.logstash.logback.argument.StructuredArguments

private val log = KotlinLogging.logger {}
object Main {
    @JvmStatic
    fun main(args: Array<String>) {
        log.error("Cake", StructuredArguments.keyValue("caketype", "chocolate"))
// Without json-config, prints: 15:56:38.134 [main] ERROR Main -- Cake
// With json-config, prints {"timestamp":"2024-11-25 15:54:30.553","level":"ERROR","message":"Cake","logger":"Main","class":"i.g.o.k.s.internal.Slf4jLogger","method":"error","line":"128","thread":"main","caketype":"chocolate"}


        log.atError {
            message = "Cake"
            payload = buildMap {
                put("caketype", "chocolate")
            }
        }
// Without json-config, prints 15:56:38.138 [main] ERROR Main -caketype="chocolate"- Cake
// With json-config, prints {"timestamp":"2024-11-25 15:54:30.565","level":"ERROR","message":"Cake","logger":"Main","class":"Main","method":"main","line":"14","thread":"main"}

    }
}

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="json" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <pattern>
                    <pattern>
                        {
                        "timestamp": "%date{yyyy-MM-dd HH:mm:ss.SSS, Europe/Oslo}",
                        "level": "%level",
                        "message": "%message",
                        "logger": "%logger{30}",
                        "class": "%class{30}",
                        "method": "%method",
                        "line": "%line",
                        "thread": "%thread{30}"
                        }
                    </pattern>
                </pattern>
                <mdc/>
                <arguments/>
            </providers>
        </encoder>
    </appender>
    <root level="debug">
        <appender-ref ref="json" />
    </root>
</configuration>

build.gradle.kts

plugins {
    kotlin("jvm") version "1.9.25"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation("io.github.oshai:kotlin-logging:7.0.0")
    implementation("org.slf4j:slf4j-api:2.0.16")
    implementation("ch.qos.logback:logback-classic:1.5.11")
    implementation("net.logstash.logback:logstash-logback-encoder:8.0")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions