'Logstash LoggingEventCompositeJsonEncoder - how omit nestedField if empty

I'm using a LoggingEventCompositeJsonEncoder as configured in this logback.xml example:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
      <providers>
        <message/>
        <loggerName>
          <fieldName>loggerName</fieldName>
        </loggerName>
        <nestedField>
          <fieldName>thrown</fieldName>
          <providers>
            <throwableClassName>
              <fieldName>name</fieldName>
            </throwableClassName>
            <stackTrace>
              <fieldName>extendedStackTrace</fieldName>
              <throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
              </throwableConverter>
            </stackTrace>
          </providers>
        </nestedField>
      </providers>
    </encoder>
  </appender>

  <root level="INFO">
    <appender-ref ref="STDOUT"/>
  </root>
</configuration>

And it's producing logs like this:

{
  "message": "Starting app",
  "loggerName": "my.api.App",
  "thrown": {}
}

I only want the thrown field output when a Throwable exists though. Is there a way to accomplish this using the provided classes in logstash-logback-encoder? If not, is there a suggestion on where a custom class/method would need to be overridden?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source