Class ShortenedThrowableConverter

All Implemented Interfaces:
ContextAware, LifeCycle

public class ShortenedThrowableConverter extends ThrowableHandlingConverter
A ThrowableHandlingConverter (similar to logback's ThrowableProxyConverter) that formats stacktraces by doing the following:
  • Limits the number of stackTraceElements per throwable (applies to each individual throwable. e.g. caused-bys and suppressed). See maxDepthPerThrowable.
  • Limits the total length in characters of the trace. See maxLength.
  • Abbreviates class names based. See setShortenedClassNameLength(int).
  • Filters out consecutive unwanted stackTraceElements based on regular expressions. See excludes.
  • Truncate individual stacktraces after any element matching one the configured regular expression. See truncateAfterPatterns.
  • Uses evaluators to determine if the stacktrace should be logged. See evaluators.
  • Outputs in either 'normal' order (root-cause-last), or root-cause-first. See rootCauseFirst.
To use this with a PatternLayout, you must configure conversionRule as described here. Options can be specified in the pattern in the following order:
  1. maxDepthPerThrowable = "full" or "short" or an integer value
  2. shortenedClassNameLength = "full" or "short" or an integer value
  3. maxLength = "full" or "short" or an integer value
The other options can be listed in any order and are interpreted as follows:
  • "rootFirst" - indicating that stacks should be printed root-cause first
  • "inlineHash" - indicating that hexadecimal error hashes should be computed and inlined
  • "inline" - indicating that the whole stack trace should be inlined, using "\\n" as separator
  • "omitCommonFrames" - omit common frames
  • "keepCommonFrames" - keep common frames
  • evaluator name - name of evaluators that will determine if the stacktrace is ignored
  • exclusion pattern - pattern for stack trace elements to exclude

For example,

 
     <conversionRule conversionWord="stack"
                   converterClass="net.logstash.logback.stacktrace.ShortenedThrowableConverter" />

     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
         <encoder>
             <pattern>[%thread] - %msg%n%stack{5,1024,10,rootFirst,omitCommonFrames,regex1,regex2,evaluatorName}</pattern>
         </encoder>
     </appender>
 
 
  • Field Details

  • Constructor Details

    • ShortenedThrowableConverter

      public ShortenedThrowableConverter()
  • Method Details

    • start

      public void start()
      Specified by:
      start in interface LifeCycle
      Overrides:
      start in class DynamicConverter<ILoggingEvent>
    • stop

      public void stop()
      Specified by:
      stop in interface LifeCycle
      Overrides:
      stop in class DynamicConverter<ILoggingEvent>
    • convert

      public String convert(ILoggingEvent event)
      Specified by:
      convert in class Converter<ILoggingEvent>
    • setLineSeparator

      public void setLineSeparator(String lineSeparator)
      Sets which lineSeparator to use between events.

      The following values have special meaning:

      • null or empty string = no new line.
      • "SYSTEM" = operating system new line (default).
      • "UNIX" = unix line ending (\n).
      • "WINDOWS" = windows line ending \r\n).

      Any other value will be used as given as the lineSeparator.

      Parameters:
      lineSeparator - the line separator
    • getLineSeparator

      public String getLineSeparator()
    • setShortenedClassNameLength

      public void setShortenedClassNameLength(int length)
      Set the length to which class names should be abbreviated. Cannot be used if a custom Abbreviator has been set through setClassNameAbbreviator(Abbreviator).
      Parameters:
      length - the desired maximum length or -1 to disable the feature and allow for any arbitrary length.
    • getShortenedClassNameLength

      public int getShortenedClassNameLength()
      Get the class name abbreviation target length. Cannot be used if a custom Abbreviator has been set through setClassNameAbbreviator(Abbreviator).
      Returns:
      the abbreviation target length
    • setClassNameAbbreviator

      public void setClassNameAbbreviator(Abbreviator abbreviator)
      Set a custom Abbreviator used to shorten class names.
      Parameters:
      abbreviator - the Abbreviator to use.
    • getClassNameAbbreviator

      public Abbreviator getClassNameAbbreviator()
    • setMaxDepthPerThrowable

      public void setMaxDepthPerThrowable(int maxDepthPerThrowable)
      Set a limit on the number of stackTraceElements per throwable. Use -1 to disable the feature and allow for an unlimited depth.
      Parameters:
      maxDepthPerThrowable - the maximum number of stacktrace elements per throwable or -1 to disable the feature and allows for an unlimited amount.
    • getMaxDepthPerThrowable

      public int getMaxDepthPerThrowable()
    • setMaxLength

      public void setMaxLength(int maxLength)
      Set a hard limit on the size of the rendered stacktrace, all throwables included. Use -1 to disable the feature and allows for any size.
      Parameters:
      maxLength - the maximum size of the rendered stacktrace or -1 for no limit.
    • getMaxLength

      public int getMaxLength()
    • setOmitCommonFrames

      public void setOmitCommonFrames(boolean omitCommonFrames)
      Control whether common frames should be omitted for nested throwables or not.
      Parameters:
      omitCommonFrames - true to omit common frames
    • isOmitCommonFrames

      public boolean isOmitCommonFrames()
    • isRootCauseFirst

      public boolean isRootCauseFirst()
    • setRootCauseFirst

      public void setRootCauseFirst(boolean rootCauseFirst)
    • isInlineHash

      public boolean isInlineHash()
    • setInlineHash

      public void setInlineHash(boolean inlineHash)
    • addExclude

      public void addExclude(String exclusionPattern)
    • addExclusions

      public void addExclusions(String commaSeparatedPatterns)
      Add multiple exclusion patterns as a list of comma separated patterns
      Parameters:
      commaSeparatedPatterns - list of comma separated patterns
    • setExcludes

      public void setExcludes(List<String> patterns)
    • getExcludes

      public List<String> getExcludes()
    • addTruncateAfter

      public void addTruncateAfter(String regex)
    • getTruncateAfters

      public List<String> getTruncateAfters()
    • addTruncateAfters

      public void addTruncateAfters(String commaSeparatedPatterns)
      Add multiple truncate after patterns as a list of comma separated patterns.
      Parameters:
      commaSeparatedPatterns - list of comma separated patterns
    • setTruncateAfters

      public void setTruncateAfters(List<String> patterns)
    • addEvaluator

      public void addEvaluator(EventEvaluator<ILoggingEvent> evaluator)
    • setEvaluators

      public void setEvaluators(List<EventEvaluator<ILoggingEvent>> evaluators)
    • getEvaluators

      public List<EventEvaluator<ILoggingEvent>> getEvaluators()