效果图
代码
package com.hxnwm.ny.diancan.common.aspect;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
/**
* @ClassName OperationLogAspect
* @Description TODO
* @Author wdj
* @Date 2023/5/31 11:23
* @Version
*/
@Aspect
@Component
@Slf4j // lombok中日志注解
public class LogAspect {
@Resource
private LogAspectMethod logAspectMethod;
/**
* 定义切入点表达式
* 访问修饰符 返回值 包名.包名.包名...类名.方法名(参数列表)
* 权限修饰符可以使用默认 第一个*表示返回值类型 ..表示当前包以及其子包下 第二个*表示任意方法 (..)表示任意参数列表
*/
private final String POINTCUT = "execution(* com.hxnwm.ny.diancan.controller..*(..))";
/**
* 前置通知,方法之前执行
* @param joinPoint
*/
@Before(POINTCUT)
public void doBefore(JoinPoint joinPoint) {
// 获取当前的HttpServletRequest对象
ServletRequestAttributes attributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
logAspectMethod.doBeforeMethod(attributes,joinPoint);
}
/**
* 返回通知 正常结束时进入此方法
*
* @param ret
*/
@AfterReturning(returning = "ret", pointcut = POINTCUT)
public void doAfterReturning(Object ret) {
logAspectMethod.doAfterReturningMethod(ret);
}
/**
* 异常通知: 1. 在目标方法非正常结束,发生异常或者抛出异常时执行
*
* @param throwable
*/
@AfterThrowing(pointcut = POINTCUT, throwing = "throwable")
public void doAfterThrowing(Throwable throwable) {
logAspectMethod.doAfterThrowingMethod(throwable);
}
}
package com.hxnwm.ny.diancan.common.aspect;
import com.hxnwm.ny.diancan.common.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Objects;
/**
* @ClassName LogAspectMethod
* @Description TODO
* @Author wdj
* @Date 2023/6/1 13:51
* @Version
*/
@Slf4j // lombok中日志注解
@Component
public class LogAspectMethod {
/**
* 进入方法时间戳
*/
private Long startTime;
/**
* 方法结束时间戳(计时)
*/
private Long endTime;
@Resource
private JwtUtils jwtUtils;
@Async("asyncExecutor")
public void doBeforeMethod(ServletRequestAttributes attributes, JoinPoint joinPoint){
if (Objects.isNull(attributes) || Objects.isNull(attributes.getRequest()) || Objects.isNull(joinPoint)) {
log.info("请求信息不能为空");
return;
}
HttpServletRequest request = attributes.getRequest();
// 打印请求的内容
startTime = System.currentTimeMillis();
Integer userId=0;//0无user
// 从请求头中获取token
String token = request.getHeader("token");
if(Objects.nonNull(token) && Objects.nonNull(jwtUtils)){
// 解密token
userId = this.jwtUtils.getUserId(token);
}
StringBuffer url=new StringBuffer();
if(Objects.nonNull(request.getRequestURL())){
url=request.getRequestURL();
}
String method= "",addr="",contentType="";
if(Objects.nonNull(request.getMethod())){
method=request.getMethod();
}
if(Objects.nonNull(request.getRemoteAddr())){
addr=request.getRemoteAddr();
}
if(Objects.nonNull(request.getContentType())){
contentType=request.getContentType();
}
log.debug("请求开始时间:{}===Url : {}===方式 : {}===ip : {}===内容类型 : {}===用户id:{}===类:[{}]===方法:[{}]===参数 : {}", LocalDateTime.now(),url.toString(),method,addr,contentType,userId,joinPoint.getSignature().getDeclaringTypeName(),joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs()));
// 系统信息
}
@Async("asyncExecutor")
public void doAfterReturningMethod(Object ret){
endTime = System.currentTimeMillis();
log.debug("请求结束时间 : {}===耗时 : {}毫秒===请求返回 : {}",LocalDateTime.now(),(endTime - startTime), ret);
}
@Async("asyncExecutor")
public void doAfterThrowingMethod(Throwable throwable){
log.error("发生异常时间 : {},抛出异常 : {}",LocalDateTime.now(),throwable.getMessage());
}
}