org.apache.logging.log4j.layout.template.json.resolver

Class ExceptionResolver

  • java.lang.Object
    • org.apache.logging.log4j.layout.template.json.resolver.ExceptionResolver
  • All Implemented Interfaces:
    EventResolver, TemplateResolver<LogEvent>
    Direct Known Subclasses:
    ExceptionRootCauseResolver


    public class ExceptionResolver
    extends Object
    implements EventResolver
    Exception resolver.

    Note that this resolver is toggled by stackTraceEnabled layout configuration, which is by default populated from log4j.layout.jsonTemplate.stackTraceEnabled system property.

    Configuration

     config              = field , [ stringified ] , [ stackTrace ]
     field               = "field" -> ( "className" | "message" | "stackTrace" )
    
     stackTrace          = "stackTrace" -> (
                             [ stringified ]
                           , [ elementTemplate ]
                           )
    
     stringified         = "stringified" -> ( boolean | truncation )
     truncation          = "truncation" -> (
                             [ suffix ]
                           , [ pointMatcherStrings ]
                           , [ pointMatcherRegexes ]
                           )
     suffix              = "suffix" -> string
     pointMatcherStrings = "pointMatcherStrings" -> string[]
     pointMatcherRegexes = "pointMatcherRegexes" -> string[]
    
     elementTemplate     = "elementTemplate" -> object
     
    stringified is set to false by default. stringified at the root level is deprecated in favor of stackTrace.stringified, which has precedence if both are provided.

    pointMatcherStrings and pointMatcherRegexes enable the truncation of stringified stack traces after the given matching point. If both parameters are provided, pointMatcherStrings will be checked first.

    If a stringified stack trace truncation takes place, it will be indicated with suffix, which by default is set to the configured truncatedStringSuffix in the layout, unless explicitly provided.

    elementTemplate is an object describing the template to be used while resolving the StackTraceElement array. If stringified is set to true, elementTemplate will be discarded. By default, elementTemplate is set to null and rather populated from the layout configuration. That is, the stack trace element template can also be provided using stackTraceElementTemplate and setStackTraceElementTemplateUri layout configuration parameters. The template to be employed is determined in the following order:

    1. elementTemplate provided in the resolver configuration
    2. stackTraceElementTemplate parameter from layout configuration (the default is populated from log4j.layout.jsonTemplate.stackTraceElementTemplate system property)
    3. stackTraceElementTemplateUri parameter from layout configuration (the default is populated from log4j.layout.jsonTemplate.stackTraceElementTemplateUri system property)

    See StackTraceElementResolver for the list of available resolvers in a stack trace element template.

    Examples

    Resolve logEvent.getThrown().getClass().getCanonicalName():
      {
       "$resolver": "exception",
       "field": "className"
     }
     
    Resolve the stack trace into a list of StackTraceElement objects:
      {
       "$resolver": "exception",
       "field": "stackTrace"
     }
     
    Resolve the stack trace into a string field:
      {
       "$resolver": "exception",
       "field": "stackTrace",
       "stackTrace": {
         "stringified": true
       }
     }
     
    Resolve the stack trace into a string field such that the content will be truncated after the given point matcher:
      {
       "$resolver": "exception",
       "field": "stackTrace",
       "stackTrace": {
         "stringified": {
           "truncation": {
             "suffix": ">",
             "pointMatcherStrings": ["at javax.servlet.http.HttpServlet.service"]
           }
         }
       }
     }
     
    Resolve the stack trace into an object described by the provided stack trace element template:
      {
       "$resolver": "exception",
       "field": "stackTrace",
       "stackTrace": {
         "elementTemplate": {
           "class": {
             "$resolver": "stackTraceElement",
             "field": "className"
           },
           "method": {
             "$resolver": "stackTraceElement",
             "field": "methodName"
           },
           "file": {
             "$resolver": "stackTraceElement",
             "field": "fileName"
           },
           "line": {
             "$resolver": "stackTraceElement",
             "field": "lineNumber"
           }
         }
       }
     }
     

    See Also:
    JsonTemplateLayout.Builder#getTruncatedStringSuffix(), JsonTemplateLayoutDefaults.getTruncatedStringSuffix(), JsonTemplateLayout.Builder#getStackTraceElementTemplate(), JsonTemplateLayoutDefaults.getStackTraceElementTemplate(), JsonTemplateLayout.Builder#getStackTraceElementTemplateUri(), JsonTemplateLayoutDefaults.getStackTraceElementTemplateUri(), ExceptionRootCauseResolver, StackTraceElementResolver
    • Method Detail

      • isResolvable

        public boolean isResolvable()
        Description copied from interface: TemplateResolver
        Indicates if the resolver if applicable at all.

        For instance, the source line resolver can be short-circuited using this check if the location information is disabled in the layout configuration.

        Specified by:
        isResolvable in interface TemplateResolver<LogEvent>
      • isResolvable

        public boolean isResolvable(LogEvent logEvent)
        Description copied from interface: TemplateResolver
        Indicates if the resolver if applicable for the given value.

        For instance, the stack trace resolver can be short-circuited using this check if the stack traces are disabled in the layout configuration.

        Specified by:
        isResolvable in interface TemplateResolver<LogEvent>

Copyright © 1999-2020 The Apache Software Foundation. All Rights Reserved.
Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, and the Apache Log4j logo are trademarks of The Apache Software Foundation.