根据个人需要修改
具体代码如下:
package com.***.dfp.rtsync.common.aspect;
import com.alibaba.fastjson.JSON;
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.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
* Description 日志切片
*
* @author Bob
* @date 2020/8/17
**/
@Component
@Aspect
@Order
@Slf4j
public class CompassAspect {
/**
* @description 切入点
* @author Bob
* @date 2020/8/25
*/
@Pointcut("@within(org.springframework.web.bind.annotation.RestController) @within(org.springframework.stereotype.Controller)")
public void logPointCut() {
}
/**
* @description Advice方式
* @author Bob
* @date 2020/8/25
*/
@Around("logPointCut()")
public Object logAround(ProceedingJoinPoint pjp) throws Throwable {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
String url = request.getRequestURI().toString();
String ip = request.getRemoteAddr();
String className = pjp.getTarget().getClass().getName();
String methodName = pjp.getSignature().getName();
log.info("##################ip:{},url:{},class:{},method:{},args:{}", ip, url, className, methodName, handlerParameter(pjp));
try {
long start = System.currentTimeMillis();
Object result = pjp.proceed();
long runTime = System.currentTimeMillis() - start;
log.info("##################runTime:{},result:{}", runTime, JSON.toJSONString(result));
return result;
} catch (RuntimeException e) {
e.printStackTrace();
return "系统开小差了";
} catch (Throwable throwable) {
throwable.printStackTrace();
return "系统异常";
}
}
/**
* @description 参数组装
* @author Bob
* @date 2020/8/25
*/
private String handlerParameter(ProceedingJoinPoint point) {
StringBuilder stringBuilder = new StringBuilder();
MethodSignature methodSignature = (MethodSignature) point.getSignature();
String[] parameterNames = methodSignature.getParameterNames();
Object[] args = point.getArgs();
int i = 0;
for (Object pojo : args) {
stringBuilder.append(parameterNames[i]).append(":").append(pojo).append(",");
}
return stringBuilder.toString();
}
}
参考:https://blog.youkuaiyun.com/lvhonglei1987/article/details/93874445