增加RestController日志
package com.olivia.sdk.aspect;
import com.olivia.sdk.ann.MethodExt;
import com.olivia.sdk.filter.LoginUser;
import com.olivia.sdk.filter.LoginUserContext;
import com.olivia.sdk.utils.JSON;
import com.olivia.sdk.utils.ReqResUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Objects;
/***
*
*/
@Slf4j
@Aspect
@Order(1)
@Component
public class WebLogAspect {
@Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
public void log() {
}
@Before("log()")
public void exPoint(JoinPoint joinPoint) throws Throwable {
}
@Around("log()")
public Object aroundLog(ProceedingJoinPoint joinPoint) throws Throwable {
if (log.isDebugEnabled()) {
HttpServletRequest request = ReqResUtils.getRequest();
List<Object> objectList = ReqResUtils.filterReqArgs(joinPoint.getArgs());
// objectList.stream().filter(t -> t instanceof BaseCheck).forEach(t -> ((BaseCheck) t).checkParam());
MethodSignature signature = ((MethodSignature) joinPoint.getSignature());
LoginUser loginUser = LoginUserContext.getLoginUser();
log.debug("url:{} reqMethod:{}.{} loginUser:{} req: {}", request.getRequestURI(), signature.getMethod().getDeclaringClass().getSimpleName(), signature.getMethod().getName(), JSON.toJSONString(loginUser), JSON.toJSONString(objectList));
Object proceed = null;
try {
proceed = joinPoint.proceed();
} finally {
MethodExt methodExt = signature.getMethod().getAnnotation(MethodExt.class);
if (Objects.isNull(methodExt) || methodExt.printResult()) {
log.debug("res:{}", JSON.toJSONString(proceed));
}
}
return proceed;
}
return joinPoint.proceed();
}
}
1147

被折叠的 条评论
为什么被折叠?



