Skip to content

Commit

Permalink
fix(log): fixes from PR comments
Browse files Browse the repository at this point in the history
- added rest of logger statements (1 per if-statement)
- used getExecutorThreadCount() to log number of threads
- moved logging utils to its own class
  • Loading branch information
diegomarquezp committed Oct 5, 2022
1 parent 8e41925 commit 854f34a
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.google.api.generator.gapic.model.GapicClass;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.GapicPackageInfo;
import com.google.api.generator.spring.composer.Utils;
import com.google.api.generator.spring.utils.Utils;
import com.google.protobuf.ByteString;
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse;
import com.google.protobuf.util.JsonFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.GapicServiceConfig;
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.spring.utils.LoggerUtils;
import com.google.api.generator.spring.utils.Utils;
import com.google.common.base.CaseFormat;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -145,39 +147,10 @@ private static List<Statement> createMemberVariables(
.build();
ExprStatement clientPropertiesStatement = ExprStatement.withExpr(clientPropertiesVarExpr);

// // private final GcpProjectIdProvider projectIdProvider;
// Variable projectIdProviderVar =
// Variable.builder()
// .setName("projectIdProvider")
// .setType(types.get("GcpProjectIdProvider"))
// .build();
// VariableExpr projectIdProviderVarExpr =
// VariableExpr.builder()
// .setVariable(projectIdProviderVar)
// .setScope(ScopeNode.PRIVATE)
// .setIsFinal(true)
// .setIsDecl(true)
// .build();
// ExprStatement projectIdProviderStatement = ExprStatement.withExpr(projectIdProviderVarExpr);

// Declare the RETRY_PARAM_DEFINITIONS map.
ExprStatement retryPramStatement =
ExprStatement.withExpr(
NESTED_RETRY_PARAM_DEFINITIONS_VAR_EXPR
.toBuilder()
.setIsDecl(true)
.setScope(ScopeNode.PRIVATE)
.setIsStatic(true)
.setIsFinal(true)
.build());

BlockStatement retryParamDefinitionsBlock =
RetrySettingsComposer.createRetryParamDefinitionsBlock(
service, serviceConfig, NESTED_RETRY_PARAM_DEFINITIONS_VAR_EXPR);

Statement loggerStatement = Utils.getLoggerDeclarationExpr(serviceName + "AutoConfig", types);
return Arrays.asList(
clientPropertiesStatement, retryPramStatement, retryParamDefinitionsBlock, loggerStatement);
Statement loggerStatement =
LoggerUtils.getLoggerDeclarationExpr(serviceName + "AutoConfig", types);
return Arrays.asList(clientPropertiesStatement, loggerStatement);
}

private static MethodDefinition createConstructor(
Expand Down Expand Up @@ -578,14 +551,12 @@ private static MethodDefinition createClientBeanMethod(
.build();

ExprStatement projectIdLoggerStatement =
Utils.createLoggerStatement(
ArithmeticOperationExpr.concatWithExprs(
ValueExpr.withValue(StringObjectValue.withValue("Quota project id set to X")),
ArithmeticOperationExpr.concatWithExprs(
getQuotaProjectId,
ValueExpr.withValue(
StringObjectValue.withValue(
", this overrides project id from credentials.")))),
LoggerUtils.createLoggerStatement(
LoggerUtils.concatManyWithExprs(
ValueExpr.withValue(StringObjectValue.withValue("Quota project id set to ")),
getQuotaProjectId,
ValueExpr.withValue(
StringObjectValue.withValue(", this overrides project id from credentials."))),
types);

IfStatement setQuotaProjectIdStatement =
Expand Down Expand Up @@ -650,22 +621,12 @@ private static MethodDefinition createClientBeanMethod(
.setArguments(executorProviderVarExpr)
.build();

MethodInvocationExpr getExecutorNameExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(executorProviderVarExpr)
.setMethodName("getClass")
.build();
getExecutorNameExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(getExecutorNameExpr)
.setMethodName("getName")
.setReturnType(TypeNode.STRING)
.build();
ExprStatement backgroundExecutorLoggerStatement =
Utils.createLoggerStatement(
LoggerUtils.createLoggerStatement(
ArithmeticOperationExpr.concatWithExprs(
ValueExpr.withValue(StringObjectValue.withValue("Background executor set to ")),
getExecutorNameExpr),
ValueExpr.withValue(
StringObjectValue.withValue("Background executor thread count is ")),
getExecutorThreadCount),
types);
IfStatement setBackgroundExecutorProviderStatement =
createIfStatement(
Expand Down Expand Up @@ -711,7 +672,14 @@ private static MethodDefinition createClientBeanMethod(
.build();
IfStatement setTransportChannelProviderStatement =
createIfStatement(
getUseRest, Arrays.asList(ExprStatement.withExpr(setTransportProvider)), null);
getUseRest,
Arrays.asList(
ExprStatement.withExpr(setTransportProvider),
LoggerUtils.createLoggerStatement(
ValueExpr.withValue(
StringObjectValue.withValue("Using HTTP transport channel")),
types)),
null);

bodyStatements.add(setTransportChannelProviderStatement);

Expand Down Expand Up @@ -768,7 +736,14 @@ private static MethodDefinition createClientBeanMethod(
IfStatement currentRetrySettingPropertyIfStatement =
createIfStatement(
currentRetrySettingPropertyIsNull,
Arrays.asList(ExprStatement.withExpr(retrySettingsBuilderChain)),
Arrays.asList(
ExprStatement.withExpr(retrySettingsBuilderChain),
LoggerUtils.createLoggerStatement(
LoggerUtils.concatManyWithExprs(
ValueExpr.withValue(
StringObjectValue.withValue(propertyName + " set to ")),
currentRetrySettingProperty),
types)),
null);
statements.add(currentRetrySettingPropertyIfStatement);
return statements;
Expand Down Expand Up @@ -1000,6 +975,8 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String
typeMap.put("ConditionalOnProperty", conditionalOnProperty);
typeMap.put("ConditionalOnClass", conditionalOnClass);
typeMap.put("Qualifier", qualifier);
typeMap.put("Log", LoggerUtils.getLoggerType());
typeMap.put("LogFactory", LoggerUtils.getLoggerFactoryType());

return typeMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.GapicServiceConfig;
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.spring.utils.Utils;
import com.google.common.base.CaseFormat;
import com.google.common.base.Joiner;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.google.api.generator.spring.utils;

import com.google.api.generator.engine.ast.ArithmeticOperationExpr;
import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.ScopeNode;
import com.google.api.generator.engine.ast.Statement;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.VaporReference;
import com.google.api.generator.engine.ast.Variable;
import com.google.api.generator.engine.ast.VariableExpr;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class LoggerUtils {

public static Statement getLoggerDeclarationExpr(String className, Map<String, TypeNode> types) {

Variable loggerVar = Variable.builder().setName("LOGGER").setType(types.get("Log")).build();
VariableExpr loggerExpr =
VariableExpr.builder()
.setVariable(loggerVar)
.setScope(ScopeNode.PRIVATE)
.setIsStatic(true)
.setIsFinal(true)
.setIsDecl(true)
.build();

MethodInvocationExpr loggerValueExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(types.get("LogFactory"))
.setMethodName("getLog")
.setArguments(
VariableExpr.builder()
.setVariable(
Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build())
.setStaticReferenceType(types.get(className))
.build())
.setReturnType(types.get("Log"))
.build();

AssignmentExpr loggerAssignmentExpr =
AssignmentExpr.builder().setVariableExpr(loggerExpr).setValueExpr(loggerValueExpr).build();

return ExprStatement.withExpr(loggerAssignmentExpr);
}

public static ExprStatement createLoggerStatement(Expr value, Map<String, TypeNode> types) {
Variable loggerVariable =
Variable.builder().setName("LOGGER").setType(types.get("Log")).build();
MethodInvocationExpr loggerCallExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(VariableExpr.withVariable(loggerVariable))
.setMethodName("info")
.setArguments(value)
.build();
return ExprStatement.withExpr(loggerCallExpr);
}

public static TypeNode getLoggerType() {
return createType("Log", "org.apache.commons.logging");
}

public static TypeNode getLoggerFactoryType() {
return createType("LogFactory", "org.apache.commons.logging");
}

private static TypeNode createType(String className, String pakkage) {
TypeNode loggerType =
TypeNode.withReference(
VaporReference.builder().setName(className).setPakkage(pakkage).build());
return loggerType;
}

public static Expr concatManyWithExprs(Expr... exprs) {
List<Expr> exprList = Arrays.asList(exprs);
return concatManyWithExprsHelper(Optional.empty(), exprList);
}

private static Expr concatManyWithExprsHelper(Optional<Expr> current, List<Expr> exprs) {
if (!current.isPresent()) {
return concatManyWithExprsHelper(Optional.of(exprs.get(0)), exprs.subList(1, exprs.size()));
}
if (exprs.size() == 1) {
return ArithmeticOperationExpr.concatWithExprs(current.get(), exprs.get(0));
}
return ArithmeticOperationExpr.concatWithExprs(
current.get(),
concatManyWithExprsHelper(Optional.of(exprs.get(0)), exprs.subList(1, exprs.size())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.generator.spring.composer;
package com.google.api.generator.spring.utils;

import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.AstNode;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.PrimitiveValue;
import com.google.api.generator.engine.ast.ScopeNode;
import com.google.api.generator.engine.ast.Statement;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.ValueExpr;
import com.google.api.generator.engine.ast.VaporReference;
import com.google.api.generator.engine.ast.Variable;
import com.google.api.generator.engine.ast.VariableExpr;
import com.google.api.generator.gapic.composer.store.TypeStore;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.GapicRetrySettings;
Expand All @@ -42,7 +35,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;

Expand Down Expand Up @@ -144,56 +136,6 @@ public static List<? extends AstNode> processRetrySettings(
return resultList;
}

public static Statement getLoggerDeclarationExpr(String className, Map<String, TypeNode> types) {

Variable loggerVar = Variable.builder().setName("LOGGER").setType(types.get("Logger")).build();
VariableExpr loggerExpr =
VariableExpr.builder()
.setVariable(loggerVar)
.setScope(ScopeNode.PRIVATE)
.setIsStatic(true)
.setIsFinal(true)
.setIsDecl(true)
.build();

MethodInvocationExpr loggerValueExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(types.get("Logger"))
.setMethodName("getLogger")
.setArguments(
VariableExpr.builder()
.setVariable(
Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build())
.setStaticReferenceType(types.get(className))
.build())
.setReturnType(types.get("Logger"))
.build();

AssignmentExpr loggerAssignmentExpr =
AssignmentExpr.builder().setVariableExpr(loggerExpr).setValueExpr(loggerValueExpr).build();

return ExprStatement.withExpr(loggerAssignmentExpr);
}

public static ExprStatement createLoggerStatement(Expr value, Map<String, TypeNode> types) {
Variable loggerVariable =
Variable.builder().setName("LOGGER").setType(types.get("Logger")).build();
MethodInvocationExpr loggerCallExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(VariableExpr.withVariable(loggerVariable))
.setMethodName("info")
.setArguments(value)
.build();
return ExprStatement.withExpr(loggerCallExpr);
}

public static TypeNode getLoggerType() {
TypeNode loggerType =
TypeNode.withReference(
VaporReference.builder().setName("Logger").setPakkage("org.slf4j").build());
return loggerType;
}

private static ValueExpr toValExpr(long longValue) {
return ValueExpr.withValue(
PrimitiveValue.builder()
Expand Down
Loading

0 comments on commit 854f34a

Please sign in to comment.