@Slf4j
@Aspect
@Component
public class CustomLogAspect {
private Gson gson = new Gson();
@Around("within(com.ypc.logtest.logback.controller..*)")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
// 目标类的名称
String className = joinPoint.getTarget().getClass().getName();
// 方法名称
String methodName = joinPoint.getSignature().getName();
// 参数列表
Parameter[] parameters = ((MethodSignature) joinPoint.getSignature()).getMethod().getParameters();
Object[] args = joinPoint.getArgs();
StringBuilder params = new StringBuilder("[");
for (int i = 0; i < parameters.length; i++) {
params.append(parameters[i].getName()).append(":").append(args[i]).append("; ");
}
if (parameters.length > 0) {
int length = params.length();
params.delete(length - 2, length);
}
params.append("]");
log.info(">>>> Target class name={} | method name={} | params={} <<<<",className,methodName,params);
Object result = null;
try {
// 执行方法
result = joinPoint.proceed();
} catch (Exception e) {
// 记录异常日志
log.error(">>>> Target class name={} | method name={}\n{}\n\t at {} <<<<",className, methodName, e.toString(), e.getStackTrace()[0]);
throw e;
}
// 记录方法结果
log.info(">>>> Target class name={} | method name={} | results={} <<<<",className, methodName, gson.toJson(result));
return result;
}
}
aop实现日志
最新推荐文章于 2024-05-18 11:48:33 发布