javalogbackspring-logbacklogstash-logback-encoder

Issues in XML Configuration For Logback


I am trying to configure LogstashTcpSocketAppender through logback-spring.xml. And when I add the lines <stackTrace> to the LogstashEncoder I get an error 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]]. If I remove <stackTrace><fieldName>stackTrace</fieldName> and only keep the <throwableConverter> it works fine. Any help is deeply appreciated.

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>

Error Log:

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

Solution

  • I believe that the <stackTrace> needs to be in a <providers> element in the encoder.

    Try

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

    More detail here: https://github.com/logstash/logstash-logback-encoder#tcp-appenders

    Or use it directly as below:

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