【log】完善aop的日志记录

pull/3/head
fengshuonan 2020-12-24 14:27:59 +08:00
parent ca9f1876a0
commit 7f05e416e1
1 changed files with 37 additions and 48 deletions

View File

@ -8,6 +8,7 @@ import cn.stylefeng.roses.kernel.log.api.factory.appender.AuthedLogAppender;
import cn.stylefeng.roses.kernel.log.api.factory.appender.HttpLogAppender;
import cn.stylefeng.roses.kernel.log.api.factory.appender.ParamsLogAppender;
import cn.stylefeng.roses.kernel.log.api.pojo.record.LogRecordDTO;
import cn.stylefeng.roses.kernel.resource.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.resource.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.resource.api.annotation.PostResource;
import lombok.extern.slf4j.Slf4j;
@ -50,7 +51,6 @@ public class RequestApiLogRecordAop implements Ordered {
*/
@Pointcut("execution(* *..controller.*.*(..))")
public void cutService() {
}
@Around("cutService()")
@ -58,10 +58,15 @@ public class RequestApiLogRecordAop implements Ordered {
Object result = point.proceed();
try {
Map<String, Object> annoProps = getAnnotationProp(point);
// 获取接口上@PostResource或者@GetResource的name属性和requiredLogin属性
Map<String, Object> annotationProp = getAnnotationProp(point);
// 获取字段的名
Map<String, Object> args = getFieldsName(point);
recordLog(args, result, annoProps);
// 记录日志
recordLog(args, result, annotationProp);
} catch (Exception e) {
log.error("日志记录没有记录成功!", e);
}
@ -70,7 +75,7 @@ public class RequestApiLogRecordAop implements Ordered {
}
/**
* AOPPostResourceGetResource
* AOP @PostResource @GetResource
*
* @param joinPoint joinPoint
* @return K, Vkeyv
@ -78,18 +83,30 @@ public class RequestApiLogRecordAop implements Ordered {
* @date 2020/12/22 21:18
*/
private Map<String, Object> getAnnotationProp(ProceedingJoinPoint joinPoint) {
MethodSignature ms = (MethodSignature) joinPoint.getSignature();
Method method = ms.getMethod();
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
// 通过map封装参数和参数值key参数名value是参数值
Map<String, Object> propMap = new HashMap<>(2);
String name = null;
String requiredLogin = null;
// 获取接口上的@PostResource或者@GetResource的name属性和requiredLogin属性填充到map
ApiResource apiResource = method.getAnnotation(ApiResource.class);
GetResource getResource = method.getAnnotation(GetResource.class);
PostResource postResource = method.getAnnotation(PostResource.class);
if (postResource == null) {
GetResource getResource = method.getAnnotation(GetResource.class);
if (getResource != null) {
propMap.put("name", getResource.name());
propMap.put("requiredLogin", getResource.requiredLogin());
}
} else {
if (apiResource != null) {
propMap.put("name", apiResource.name());
propMap.put("requiredLogin", apiResource.requiredLogin());
}
if (getResource != null) {
propMap.put("name", getResource.name());
propMap.put("requiredLogin", getResource.requiredLogin());
}
if (postResource != null) {
propMap.put("name", postResource.name());
propMap.put("requiredLogin", postResource.requiredLogin());
}
@ -97,47 +114,19 @@ public class RequestApiLogRecordAop implements Ordered {
return propMap;
}
/*
@Around("cutService()&&(@annotation(postResource))")
public Object aroundPost(ProceedingJoinPoint point, PostResource postResource) throws Throwable {
Object result = point.proceed();
try {
Map<String, Object> args = getFieldsName(point);
recordLog(args, result);
} catch (Exception e) {
log.error("日志记录没有记录成功!", e);
}
return result;
}
@Around("cutService()&&(@annotation(getResource))")
public Object aroundGet(ProceedingJoinPoint point, GetResource getResource) throws Throwable {
Object result = point.proceed();
try {
Map<String, Object> args = getFieldsName(point);
recordLog(args, result);
} catch (Exception e) {
log.error("日志记录没有记录成功!", e);
}
return result;
}*/
/**
*
*
* @param params AOPkeyv
* @param result AOP
* @param annoProps AOP
* @param params AOPkeyv
* @param result AOP
* @param annotationProp AOP
* @author fengshuonan
* @date 2020/10/28 17:38
*/
private void recordLog(Map<String, Object> params, Object result, Map<String, Object> annoProps) {
Object actionName = annoProps.get("name");
private void recordLog(Map<String, Object> params, Object result, Map<String, Object> annotationProp) {
Object actionName = annotationProp.get("name");
// 创建日志对象
LogRecordDTO logRecordDTO = LogRecordFactory.createLogRecord(LogConstants.LOG_DEFAULT_NAME, actionName);