Package groovy.util.logging
Class Log4j2.Log4j2LoggingStrategy
java.lang.Object
org.codehaus.groovy.transform.LogASTTransformation.AbstractLoggingStrategy
org.codehaus.groovy.transform.LogASTTransformation.AbstractLoggingStrategyV2
groovy.util.logging.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.
-
Field Summary
Fields inherited from class org.codehaus.groovy.transform.LogASTTransformation.AbstractLoggingStrategy
loader -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreates a Log4j 2 logging strategy. -
Method Summary
Modifier and TypeMethodDescriptionaddLoggerFieldToClass(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.booleanisLoggingMethod(String methodName) Reports whethermethodNameis a logging call this strategy rewrites.WhyLogASTTransformation.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 whenstaticLocationis requested anyway.wrapLoggingMethodCall(Expression logVariable, String methodName, Expression originalExpression) Wraps a logging call in the guard this strategy uses.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).Methods inherited from class org.codehaus.groovy.transform.LogASTTransformation.AbstractLoggingStrategyV2
addLoggerFieldToClassMethods inherited from class org.codehaus.groovy.transform.LogASTTransformation.AbstractLoggingStrategy
classNodeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.codehaus.groovy.transform.LogASTTransformation.LoggingStrategy
getCategoryName
-
Constructor Details
-
Log4j2LoggingStrategy
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 fieldcategoryName- the name of the logging categoryfieldModifiers- the modifiers (private, final, et. al.) of the logger field- Returns:
- the FieldNode instance that was created and added to the class
-
isLoggingMethod
Reports whethermethodNameis a logging call this strategy rewrites.- Parameters:
methodName- the method name on the logger, such asinfo- Returns:
truewhen a call of that name should be wrapped
-
wrapLoggingMethodCall
public Expression wrapLoggingMethodCall(Expression logVariable, String methodName, Expression originalExpression) Wraps a logging call in the guard this strategy uses.- Parameters:
logVariable- the logger expressionmethodName- the logging method that was calledoriginalExpression- the call as written- Returns:
- the replacement expression
-
staticLocationUnsupportedReason
WhyLogASTTransformation.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 whenstaticLocationis requested anyway. A strategy whose logging API accepts a caller-supplied location returnsnullwhen 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
LogBuilderto be resolvable.- Returns:
- the reason compile-time locations are unavailable, or
nullif 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 whenLogASTTransformation.LoggingStrategy.staticLocationUnsupportedReason()returnednull.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 leadingMarkergoes throughwithMarkerand a trailingThrowablethroughwithThrowable, which the builder'slogmethods do not do on their own. Non-simple arguments keep theisLevelEnabledguard so they are not evaluated for a disabled level.- Parameters:
logVariable- the logger expressionmethodName- the logging method that was called, e.g.infooriginalCall- the call as written; its arguments are the call's argumentslocation- an expression yielding theStackTraceElementfor the statementguard- whether the arguments are worth guarding with a level check (they are not all constants or variables)- Returns:
- the replacement expression, or
nullto leave the call unchanged
-