Class Log4j2.Log4j2LoggingStrategy

All Implemented Interfaces:
LogASTTransformation.LoggingStrategy, LogASTTransformation.LoggingStrategyV2
Enclosing class:
Log4j2

public static class Log4j2.Log4j2LoggingStrategy extends LogASTTransformation.AbstractLoggingStrategyV2
Logging strategy for Log4j 2.
  • Constructor Details

    • Log4j2LoggingStrategy

      protected Log4j2LoggingStrategy(GroovyClassLoader loader)
      Creates a Log4j 2 logging strategy.
      Parameters:
      loader - the class loader used to resolve logger classes
  • Method Details

    • addLoggerFieldToClass

      public FieldNode addLoggerFieldToClass(ClassNode classNode, String logFieldName, String categoryName, int fieldModifiers)
      In this method, you are given a ClassNode, a field name and a category name, and you must add a new Field onto the class. Return the result of the ClassNode.addField operations.
      Parameters:
      classNode - the class that was originally annotated with the Log transformation.
      logFieldName - the name of the logger field
      categoryName - the name of the logging category
      fieldModifiers - the modifiers (private, final, et. al.) of the logger field
      Returns:
      the FieldNode instance that was created and added to the class
    • isLoggingMethod

      public boolean isLoggingMethod(String methodName)
    • wrapLoggingMethodCall

      public Expression wrapLoggingMethodCall(Expression logVariable, String methodName, Expression originalExpression)
    • staticLocationUnsupportedReason

      public String staticLocationUnsupportedReason()
      Why LogASTTransformation.LoggingStrategy.wrapLoggingMethodCallWithLocation(org.codehaus.groovy.ast.expr.Expression, java.lang.String, org.codehaus.groovy.ast.expr.MethodCallExpression, org.codehaus.groovy.ast.expr.Expression, boolean) cannot be used with this strategy in the current compilation, for the compile error reported when staticLocation is requested anyway. A strategy whose logging API accepts a caller-supplied location returns null when that API is resolvable, and names what is missing from the compile classpath when it is not. By default compile-time locations are not implemented.

      Requires Log4j 2.13's LogBuilder to be resolvable.

      Returns:
      the reason compile-time locations are unavailable, or null if they are supported
    • wrapLoggingMethodCallWithLocation

      public Expression wrapLoggingMethodCallWithLocation(Expression logVariable, String methodName, MethodCallExpression originalCall, Expression location, boolean guard)
      Rewrites a logging call so that the logging framework is handed the statement's location, computed at compile time, instead of walking the stack at run time (GROOVY-12378). Only called when LogASTTransformation.LoggingStrategy.staticLocationUnsupportedReason() returned null.

      Emits log.atLevel().withLocation(location).log(args). Argument types are unknown at compile time, so when a call has several arguments and its first or last one is a variable, that variable is tested at run time: a leading Marker goes through withMarker and a trailing Throwable through withThrowable, which the builder's log methods do not do on their own. Non-simple arguments keep the isLevelEnabled guard so they are not evaluated for a disabled level.

      Parameters:
      logVariable - the logger expression
      methodName - the logging method that was called, e.g. info
      originalCall - the call as written; its arguments are the call's arguments
      location - an expression yielding the StackTraceElement for the statement
      guard - whether the arguments are worth guarding with a level check (they are not all constants or variables)
      Returns:
      the replacement expression, or null to leave the call unchanged