Annotation Interface Log4j2


@Documented @Retention(SOURCE) @Target(TYPE) public @interface Log4j2
This local transform adds a logging ability to your program using Log4j2 logging. Every method call on an unbound variable named log will be mapped to a call to the logger. For this a log field will be inserted in the class. If the field already exists the usage of this transform will cause a compilation error. The method name will be used to determine what to call on the logger.
 log.name(exp)
 
is mapped to
 if (log.isNameEnabled() {
    log.name(exp)
 }
Here name is a placeholder for info, debug, warning, error, etc. If the expression exp is a constant or only a variable access the method call will not be transformed. But this will still cause a call on the injected logger.
Since:
2.2.0
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Logging strategy for Log4j 2.
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Returns the logger category name.
    Returns the logging strategy implementation used by this transform.
    boolean
    Whether logging statements hand Log4j 2 their source location, computed at compile time, instead of leaving it to be found by walking the stack at run time.
    Returns the injected logger field name.
    If specified, must match the "id" attribute in a VisibilityOptions annotation to enable a custom visibility.
  • Element Details

    • value

      String value
      Returns the injected logger field name. Defaults to "log".
      Returns:
      the logger field name
      Default:
      "log"
    • category

      String category
      Returns the logger category name. Defaults to LogASTTransformation.DEFAULT_CATEGORY_NAME, which uses the host class name.
      Returns:
      the logger category name
      Default:
      "##default-category-name##"
    • visibilityId

      String visibilityId
      If specified, must match the "id" attribute in a VisibilityOptions annotation to enable a custom visibility.
      Since:
      3.0.0
      Default:
      "<DummyUndefinedMarkerString-DoNotUse>"
    • loggingStrategy

      Class<? extends LogASTTransformation.LoggingStrategy> loggingStrategy
      Returns the logging strategy implementation used by this transform. Defaults to Log4j2.Log4j2LoggingStrategy.
      Returns:
      the logging strategy type
      Default:
      groovy.util.logging.Log4j2.Log4j2LoggingStrategy.class
    • staticLocation

      boolean staticLocation
      Whether logging statements hand Log4j 2 their source location, computed at compile time, instead of leaving it to be found by walking the stack at run time. With true, log.info(msg) becomes log.atInfo().withLocation(location).log(msg), where location is a StackTraceElement for the statement held in a synthetic static field of the annotated class, naming the class, the enclosing method, the source file and the line of the statement. That location is correct however the call is dispatched -- through the Groovy runtime on a JVM, or inside a GraalVM native image, where stack walking otherwise reports runtime frames -- and it spares Log4j 2 the stack walk that %C, %M, %F and %L patterns need.

      Requires the LogBuilder API (Log4j 2.13 or later) on the compile classpath; compilation fails otherwise. Argument types are unknown when the transform runs, so a Marker first argument or a Throwable last argument is recognised, at run time, when it is passed as a variable (the usual log.error("...", e) shape); other expressions in those positions are logged as message parameters. Statements inside a closure name the enclosing method rather than the closure's doCall. Only statements made through the injected logger field are rewritten.

      Returns:
      true to supply locations at compile time
      Since:
      6.0.0
      Default:
      false