package com.huadian.commons.aop;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Configuration;
@Aspect
@Configuration
@Slf4j
public class WebLogAspect {
ObjectMapper objectMapper = new ObjectMapper();
/**
* 定义切点 切点为com.gongj.mall.product.scenario.controller下所有的类
* 其中类里的所有方法为连接点
*/
@Pointcut("execution(* com.chenye.*.*.rest..*.*(..))")
public void webLog() {
// Do nothing because of X and Y.
}
/**
* 环绕通知
*/
@Around(value = "webLog()")
public Object webLogAround(ProceedingJoinPoint joinPoint) throws Throwable {
//环绕通知try catch 异常处理应该抛出 hwh 2022-05-22
// try {
// className的值:com.gongj.test.controller.UserController
String className = joinPoint.getTarget().getClass().getName();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
// 方法名称示例:test.controller.UserController.getUser():
String methodName = new StringBuffer(className.replaceFirst("test.", ""))
.append(".")
.append(signature.getMethod().getName())
.append("():").toString();
Object proceed = joinPoint.proceed();
Object[] objects = joinPoint.getArgs();
try {
log.debug("==========> {} 方法原始报文:{}",
methodName, objectMapper.writeValueAsString(objects[1]));
log.debug("==========> {} 方法响应报文:{}",
methodName, objectMapper.writeValueAsString(proceed));
} catch (Exception e) {
log.error(e.getMessage());
}
return proceed;
// } catch (Exception e1) {
// log.error(e1.getMessage());
// }
// return null;
}
}
springboot 所有接口日志
最新推荐文章于 2024-07-19 10:32:51 发布