import cn.hutool.json.JSONUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.StringJoiner;
@Aspect
@Slf4j
@Component
public class PrintLogAop {
//切点
@Pointcut("execution(public * com.xxx.controller..*.*(..))")
public void pointcut() {
}
@Around(value = "pointcut()")
public Object around(ProceedingJoinPoint joinPoint) throws Exception {
StringBuilder logStr = new StringBuilder();
//请求url数据
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
logStr.append("请求者ip:").append(getIpAddress(request)).append(",");
logStr.append("请求地址:").append(request.getRequestURL()).append(",");
Signature signature = joinPoint.getSignature();
//获取类信息
Class clazz = signature.getDeclaringType();
logStr.append("类:").append(clazz.getName()).append(",");
logStr.append("方法:").append(signature.getName()).append(",");
//获取参数
Object[] args = joinPoint.getArgs();
StringJoiner param = new StringJoiner("|");
for (Object obj : args) {
param.add(formatObject(obj));
}
logStr.append("参数:").append(param).append(",");
Long start = System.currentTimeMillis();
try {
//执行方法
Object obj = joinPoint.proceed();
logStr.append("返回结果:").append(formatObject(obj)).append(",");
return obj;
} catch (Throwable throwable) {
//处理自定义异常
//if (throwable instanceof BusinessException) {
// throw (BusinessException) throwable;
//} else {
throw (Exception) throwable;
//}
} finally {
//打印日志
Long end = System.currentTimeMillis();
log.info(logStr + "时间:" + (end - start) + "ms");
}
}
/**
* 格式化obj
* @param obj
* @return
*/
private String formatObject(Object obj) {
if(null == obj){
return null;
}
if (obj instanceof String || obj instanceof Long || obj instanceof Integer || obj instanceof Boolean) {
return (String) obj;
} else {
return JSONUtil.toJsonStr(obj);
}
}
/**
* 获取ip
* @param request
* @return
*/
public String getIpAddress(HttpServletRequest request) {
String ipAddress = request.getHeader("X-Forwarded-For");
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_CLIENT_IP");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
}
return ipAddress;
}
}