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