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

Add list attribute to @LogRecord annotation #175

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion bizlog-sdk/src/main/java/com/mzt/logapi/beans/LogRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,9 @@ public class LogRecord {
* CodeVariableType 日志记录的ClassName、MethodName
*/
private Map<CodeVariableType, Object> codeVariable;
}

/**
* list变量的名称
*/
private String list;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ public class LogRecordOps {
private String extra;
private String condition;
private String isSuccess;
private String list;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ public interface ILogRecordService {
*/
void record(LogRecord logRecord);

/**
* 批量保存log
*
* @param logRecords 日志实体列表
*/
void recordBatch(List<LogRecord> logRecords);

/**
* 返回最多100条记录
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@
* @return 表示成功的表达式,默认为空,代表不抛异常为成功
*/
String successCondition() default "";

/**
* @return list变量的名称
*/
String list() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,17 @@ private void saveLog(Method method, boolean flag, LogRecordOps operation, String
.operator(getRealOperatorId(operation, operatorIdFromService, expressionValues))
.subType(expressionValues.get(operation.getSubType()))
.extra(expressionValues.get(operation.getExtra()))
.codeVariable(getCodeVariable(method))
.codeVariable(getCodeVariableMap(method))
.action(expressionValues.get(action))
.fail(flag)
.createTime(new Date())
.list(expressionValues.get(operation.getList()))
.build();

bizLogService.record(logRecord);
}

private Map<CodeVariableType, Object> getCodeVariable(Method method) {
private Map<CodeVariableType, Object> getCodeVariableMap(Method method) {
Map<CodeVariableType, Object> map = new HashMap<>();
map.put(CodeVariableType.ClassName, method.getDeclaringClass());
map.put(CodeVariableType.MethodName, method.getName());
Expand All @@ -225,6 +226,7 @@ private List<String> getSpElTemplates(LogRecordOps operation, String... actions)
spElTemplates.add(operation.getBizNo());
spElTemplates.add(operation.getSubType());
spElTemplates.add(operation.getExtra());
spElTemplates.add(operation.getList());
spElTemplates.addAll(Arrays.asList(actions));
return spElTemplates;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ private LogRecordOps parseLogRecordAnnotation(AnnotatedElement ae, LogRecord rec
.extra(recordAnnotation.extra())
.condition(recordAnnotation.condition())
.isSuccess(recordAnnotation.successCondition())
.list(recordAnnotation.list())
.build();
validateLogRecordOperation(ae, recordOps);
return recordOps;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.mzt.logapi.starter.support.parse;

import com.mzt.logapi.beans.MethodExecuteResult;
import com.mzt.logapi.beans.LogRecord;
import com.mzt.logapi.beans.LogRecordOps;
import com.mzt.logapi.context.LogRecordContext;
import com.mzt.logapi.service.impl.DiffParseFunction;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.expression.AnnotatedElementKey;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;
import java.util.*;
Expand Down Expand Up @@ -140,6 +145,79 @@ public Map<String, String> processBeforeExecuteFunctionTemplate(Collection<Strin
return functionNameAndReturnValueMap;
}

public List<LogRecord> processLogRecordOps(List<LogRecordOps> logRecordOpsList, MethodExecuteResult methodExecuteResult,
Map<String, String> beforeFunctionNameAndReturnMap) {
List<LogRecord> logRecords = new ArrayList<>();
for (LogRecordOps logRecordOps : logRecordOpsList) {
if (StringUtils.isEmpty(logRecordOps.getList())) {
logRecords.add(processSingleLogRecordOps(logRecordOps, methodExecuteResult, beforeFunctionNameAndReturnMap));
} else {
logRecords.addAll(processListLogRecordOps(logRecordOps, methodExecuteResult, beforeFunctionNameAndReturnMap));
}
}
return logRecords;
}

private LogRecord processSingleLogRecordOps(LogRecordOps logRecordOps, MethodExecuteResult methodExecuteResult,
Map<String, String> beforeFunctionNameAndReturnMap) {
Map<String, String> expressionValues = processTemplate(getSpElTemplates(logRecordOps), methodExecuteResult, beforeFunctionNameAndReturnMap);
return buildLogRecord(logRecordOps, expressionValues, methodExecuteResult.getMethod());
}

private List<LogRecord> processListLogRecordOps(LogRecordOps logRecordOps, MethodExecuteResult methodExecuteResult,
Map<String, String> beforeFunctionNameAndReturnMap) {
List<LogRecord> logRecords = new ArrayList<>();
EvaluationContext evaluationContext = expressionEvaluator.createEvaluationContext(methodExecuteResult.getMethod(),
methodExecuteResult.getArgs(), methodExecuteResult.getTargetClass(), methodExecuteResult.getResult(),
methodExecuteResult.getErrorMsg(), beanFactory);
AnnotatedElementKey annotatedElementKey = new AnnotatedElementKey(methodExecuteResult.getMethod(), methodExecuteResult.getTargetClass());
Object listObject = expressionEvaluator.parseExpression(logRecordOps.getList(), annotatedElementKey, evaluationContext);
if (listObject instanceof Collection) {
Collection<?> list = (Collection<?>) listObject;
for (Object item : list) {
StandardEvaluationContext itemContext = new StandardEvaluationContext(item);
itemContext.setVariables(evaluationContext.getVariables());
Map<String, String> expressionValues = processTemplate(getSpElTemplates(logRecordOps), methodExecuteResult, beforeFunctionNameAndReturnMap);
logRecords.add(buildLogRecord(logRecordOps, expressionValues, methodExecuteResult.getMethod()));
}
}
return logRecords;
}

private List<String> getSpElTemplates(LogRecordOps logRecordOps) {
List<String> spElTemplates = new ArrayList<>();
spElTemplates.add(logRecordOps.getType());
spElTemplates.add(logRecordOps.getBizNo());
spElTemplates.add(logRecordOps.getSubType());
spElTemplates.add(logRecordOps.getExtra());
spElTemplates.add(logRecordOps.getList());
spElTemplates.add(logRecordOps.getSuccessLogTemplate());
spElTemplates.add(logRecordOps.getFailLogTemplate());
return spElTemplates;
}

private LogRecord buildLogRecord(LogRecordOps logRecordOps, Map<String, String> expressionValues, Method method) {
return LogRecord.builder()
.tenant(expressionValues.get("tenant"))
.type(expressionValues.get(logRecordOps.getType()))
.bizNo(expressionValues.get(logRecordOps.getBizNo()))
.operator(expressionValues.get(logRecordOps.getOperatorId()))
.subType(expressionValues.get(logRecordOps.getSubType()))
.extra(expressionValues.get(logRecordOps.getExtra()))
.codeVariable(getCodeVariable(method))
.action(expressionValues.get(logRecordOps.getSuccessLogTemplate()))
.fail(false)
.createTime(new Date())
.list(expressionValues.get(logRecordOps.getList()))
.build();
}

private Map<CodeVariableType, Object> getCodeVariable(Method method) {
Map<CodeVariableType, Object> map = new HashMap<>();
map.put(CodeVariableType.ClassName, method.getDeclaringClass());
map.put(CodeVariableType.MethodName, method.getName());
return map;
}

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
Expand Down