logbackslf4j

Can I override portions of an appender defined in a logback include file?


I asked a related question several months ago, but I didn't get any response: Can logback.xml reference a property in a provider declaration? .

I maintain the Java platform for a large number of services running SpringBoot in Kubernetes. All of these services use slf4j/logback. Each service has a "logback.xml" defined in the source code of each service, and each one of them has an "include" for a common file that is defined outside of each service (made available in a k8s configmap).

In the "base" logback, we define a STDOUT ConsoleAppender with a lot of properties, including an encoder (LoggingEventCompositeJsonEncoder) that has a bunch of provider properties, including a "stackTrace" element that looks like this (so I can say I posted some code here):

            <stackTrace>
                <fieldName>exTrace</fieldName>
                <throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
                    <maxDepthPerThrowable>10</maxDepthPerThrowable>
                    <rootCauseFirst>false</rootCauseFirst>
                    <maxLength>10240</maxLength>
                </throwableConverter>
            </stackTrace>

In the "logback.xml" of the service, we define some loggers that can be customized in the service.

I need to examine options for how I can customize that "stackTrace" block in each service.

If I simply pasted the entire appender definition in the "logback.xml" right after the "include" statement, would it replace the appender I defined in the "base" logback?

Is there some way to keep the one in the "base" logback, but simply specify in the service's "logback.xml" that it needs to override some properties in the provider, like the "stackTrace" element?


Solution

  • I don't remember where I found the answer, but I was able to resolve this.

    In the "baselogback.xml", we changed this block to the following:

                <stackTrace>
                    <fieldName>exTrace</fieldName>
                    <throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
                        <maxDepthPerThrowable>${maxDepthPerThrowable:-10}</maxDepthPerThrowable>
                        <rootCauseFirst>${rootCauseFirst:-false}</rootCauseFirst>
                        <maxLength>${maxLength:-10240}</maxLength>
                    </throwableConverter>
                </stackTrace>
    

    In each service, before they "include" the "baselogback.xml" file, they have a line like this:

    That file in each service is just a properties file. If they define properties like this:

    #Custom logback.xml properties
    maxDepthPerThrowable    = 12
    rootCauseFirst          = true
    maxLength               = 12000
    

    Then those values will override the defaults set in the "baselogback.xml". If the properties file isn't present, or they simply don't define those properties, it will just use the default values.