XML Logback 配置中的问题

Issues in XML Configuration For Logback

我正在尝试通过 logback-spring.xml 配置 LogstashTcpSocketAppender。当我将 <stackTrace> 行添加到 LogstashEncoder 时,出现错误 Error is: "weblogic.application.ModuleException: java.lang.IllegalStateException: Logback configuration error detected: ERROR in ch.qos.logback.core.joran.spi.Interpreter@51:25 - no applicable action for [stackTrace], current ElementPath is [[configuration][appender][encoder][stackTrace]]。如果我删除 <stackTrace><fieldName>stackTrace</fieldName> 并只保留 <throwableConverter> 它工作正常。非常感谢任何帮助。

logback-spring.xml

<appender name="LOGSTASH"
              class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <destination>${LOGSTASH_HOST}:${LOGSTASH_PORT}</destination>
        <encoder charset="UTF-8"
                 class="net.logstash.logback.encoder.LogstashEncoder">
            <stackTrace>
                <fieldName>stackTrace</fieldName>
                <throwableConverter
                        class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
                    <rootCauseFirst>true</rootCauseFirst>
                </throwableConverter>
            </stackTrace>
        </encoder>
    </appender>

    <appender name="ASYNC_LOGSTASH"
              class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="LOGSTASH"/>
    </appender>

错误日志:

Caused By: java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.joran.spi.Interpreter@51:25 - no applicable action for [stackTrace], current ElementPath  is [[configuration][appender][encoder][stackTrace]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@52:28 - no applicable action for [fieldName], current ElementPath  is [[configuration][appender][encoder][stackTrace][fieldName]]
        at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:166)
        at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:82)
        at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:60)
        at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:114)
        at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:264)
        Truncated. see log file for complete stacktrace

我认为 <stackTrace> 需要在编码器的 <providers> 元素中。

尝试

<appender name="LOGSTASH"
          class="net.logstash.logback.appender.LogstashTcpSocketAppender">
    <destination>${LOGSTASH_HOST}:${LOGSTASH_PORT}</destination>
    <encoder charset="UTF-8"
             class="net.logstash.logback.encoder.LogstashEncoder">
       <providers>
        <stackTrace>
            <fieldName>stackTrace</fieldName>
            <throwableConverter
                    class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
                <rootCauseFirst>true</rootCauseFirst>
            </throwableConverter>
        </stackTrace>
       </providers>
    </encoder>
</appender>

<appender name="ASYNC_LOGSTASH"
          class="ch.qos.logback.classic.AsyncAppender">
    <appender-ref ref="LOGSTASH"/>
</appender>

这里有更多细节: https://github.com/logstash/logstash-logback-encoder#tcp-appenders

或直接如下使用:

<encoder class="net.logstash.logback.encoder.LogstashEncoder">
  <throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
    <maxDepthPerThrowable>30</maxDepthPerThrowable>
    <maxLength>2048</maxLength>
    <shortenedClassNameLength>20</shortenedClassNameLength>
    <exclude>sun\.reflect\..*\.invoke.*</exclude>
    <exclude>net\.sf\.cglib\.proxy\.MethodProxy\.invoke</exclude>
    <evaluator class="myorg.MyCustomEvaluator"/>
    <rootCauseFirst>true</rootCauseFirst>
    <inlineHash>true</inlineHash>
  </throwableConverter>
</encoder>