你可以覆盖在logback包含文件中定义的appender的部分吗?

huangapple go评论55阅读模式
英文:

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

问题

我几个月前曾提出一个相关问题,但没有收到任何回应:https://stackoverflow.com/questions/74351542/can-logback-xml-reference-a-property-in-a-provider-declaration。

我维护着运行在Kubernetes中的大量SpringBoot服务的Java平台。所有这些服务都使用slf4j/logback。每个服务在源代码中都定义了一个名为“logback.xml”的文件,而且每个服务都有一个“include”,引用了一个在每个服务之外定义的共享文件(在k8s configmap中提供)。

在“base” logback中,我们定义了一个具有许多属性的STDOUT ConsoleAppender,其中包括一个编码器(LoggingEventCompositeJsonEncoder),该编码器具有一些提供程序属性,包括一个看起来像这样的“stackTrace”元素(这样我可以说我在这里发布了一些代码):

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

在服务的“logback.xml”中,我们定义了一些可以在服务中自定义的记录器。

我需要研究一下如何在每个服务中自定义那个“stackTrace”块的选项。

如果我只是在“include”语句后面将整个appender定义粘贴到“logback.xml”中,是否会替换我在“base” logback中定义的appender?

是否有一种方法可以保留“base” logback中的定义,但只是在服务的“logback.xml”中指定它需要覆盖提供程序中的一些属性,比如“stackTrace”元素?

英文:

I asked a related question several months ago, but I didn't get any response: https://stackoverflow.com/questions/74351542/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):

            &lt;stackTrace&gt;
                &lt;fieldName&gt;exTrace&lt;/fieldName&gt;
                &lt;throwableConverter class=&quot;net.logstash.logback.stacktrace.ShortenedThrowableConverter&quot;&gt;
                    &lt;maxDepthPerThrowable&gt;10&lt;/maxDepthPerThrowable&gt;
                    &lt;rootCauseFirst&gt;false&lt;/rootCauseFirst&gt;
                    &lt;maxLength&gt;10240&lt;/maxLength&gt;
                &lt;/throwableConverter&gt;
            &lt;/stackTrace&gt;

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?

答案1

得分: 0

我不记得在哪里找到答案,但我能够解决这个问题。

在“baselogback.xml”中,我们将此块更改为以下内容:

<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>

在每个服务中,在他们“包含”“baselogback.xml”文件之前,他们有这样一行:

<property file="opt/ajsc/etc/config/logback-custom.properties"/>

每个服务中的该文件只是一个属性文件。如果他们定义了这样的属性:

#自定义logback.xml属性
maxDepthPerThrowable = 12
rootCauseFirst = true
maxLength = 12000

那么这些值将覆盖“baselogback.xml”中设置的默认值。如果属性文件不存在,或者他们根本没有定义这些属性,它将使用默认值。

英文:

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:

            &lt;stackTrace&gt;
                &lt;fieldName&gt;exTrace&lt;/fieldName&gt;
                &lt;throwableConverter class=&quot;net.logstash.logback.stacktrace.ShortenedThrowableConverter&quot;&gt;
                    &lt;maxDepthPerThrowable&gt;${maxDepthPerThrowable:-10}&lt;/maxDepthPerThrowable&gt;
                    &lt;rootCauseFirst&gt;${rootCauseFirst:-false}&lt;/rootCauseFirst&gt;
                    &lt;maxLength&gt;${maxLength:-10240}&lt;/maxLength&gt;
                &lt;/throwableConverter&gt;
            &lt;/stackTrace&gt;

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

<property file="opt/ajsc/etc/config/logback-custom.properties"/>

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.

huangapple
  • 本文由 发表于 2023年6月8日 07:50:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定