Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(log): added log statements to gend autocfg #1053

Merged
merged 5 commits into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -19,6 +19,7 @@
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.api.generator.engine.ast.AnnotationNode;
import com.google.api.generator.engine.ast.ArithmeticOperationExpr;
import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.CastExpr;
import com.google.api.generator.engine.ast.ClassDefinition;
Expand Down Expand Up @@ -47,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 @@ -157,7 +160,9 @@ private static List<Statement> createMemberVariables(
.build();
ExprStatement clientPropertiesStatement = ExprStatement.withExpr(clientPropertiesVarExpr);

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

private static MethodDefinition createConstructor(
Expand Down Expand Up @@ -559,10 +564,19 @@ private static MethodDefinition createClientBeanMethod(
.setArguments(getQuotaProjectId)
.build();

ExprStatement projectIdLoggerStatement =
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 =
createIfStatement(
projectIdIsNull,
Arrays.asList(ExprStatement.withExpr(setQuotaProjectId)), // TODO add logger info
Arrays.asList(ExprStatement.withExpr(setQuotaProjectId), projectIdLoggerStatement),
null);

bodyStatements.add(setQuotaProjectIdStatement);
Expand Down Expand Up @@ -621,12 +635,20 @@ private static MethodDefinition createClientBeanMethod(
.setArguments(executorProviderVarExpr)
.build();

ExprStatement backgroundExecutorLoggerStatement =
LoggerUtils.createLoggerStatement(
ArithmeticOperationExpr.concatWithExprs(
ValueExpr.withValue(
StringObjectValue.withValue("Background executor thread count is ")),
getExecutorThreadCount),
types);
IfStatement setBackgroundExecutorProviderStatement =
createIfStatement(
executorThreadCountIsNull,
Arrays.asList(
ExprStatement.withExpr(executorProviderAssignExpr),
ExprStatement.withExpr(setBackgroundExecutorProvider)), // TODO add logger info
ExprStatement.withExpr(setBackgroundExecutorProvider),
backgroundExecutorLoggerStatement),
null);

bodyStatements.add(setBackgroundExecutorProviderStatement);
Expand Down Expand Up @@ -664,7 +686,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 @@ -721,7 +750,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 @@ -947,6 +983,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
109 changes: 109 additions & 0 deletions src/main/java/com/google/api/generator/spring/utils/LoggerUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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,7 +12,7 @@
// 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.AstNode;
import com.google.api.generator.engine.ast.Expr;
Expand Down
Loading