接口操作日志/功能日志AOP初版
parent
d6e44ecb2d
commit
27bf504f3a
|
@ -0,0 +1,66 @@
|
|||
package com.god.common.log.aspect;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @description: 接口操作日志
|
||||
* @Author fst
|
||||
* @date 2023/11/22 13:45
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
public class AopLog {
|
||||
/**
|
||||
* 日志打印镀锡
|
||||
*/
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
ThreadLocal<Long> startTime = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 定义切点,打印所有接口
|
||||
*/
|
||||
@Pointcut(value = "execution(* com.god.*.server.controller.*.*(..))")
|
||||
public void aopWebLog(){};
|
||||
|
||||
|
||||
/**
|
||||
* 使用环绕通知
|
||||
*/
|
||||
@Around("aopWebLog()")
|
||||
public Object myLogger(ProceedingJoinPoint pjp) throws Throwable {
|
||||
startTime.set(System.currentTimeMillis());
|
||||
//使用ServletRequestAttributes请求上下文获取更多方法
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
assert attributes != null;
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
//通过pjp获取接口信息
|
||||
String className = pjp.getSignature().getDeclaringTypeName(); //类名称
|
||||
String methodName = pjp.getSignature().getName(); //方法名称
|
||||
//获取参数
|
||||
Object[] args = pjp.getArgs();
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
//执行接口前打印日志
|
||||
logger.info("==============开始执行:{}: {}接口===================",className,methodName);
|
||||
logger.info("传递的参数为:{}",mapper.writeValueAsString(args));
|
||||
logger.info("RUL:{}",request.getRequestURL().toString());
|
||||
logger.info("IP地址为:{}",request.getRemoteAddr());
|
||||
Object obj = pjp.proceed();
|
||||
//执行完打印日志
|
||||
logger.info("=============={}: {}接口执行成功===================",className,methodName);
|
||||
logger.info("耗时:{}ms",System.currentTimeMillis()-startTime.get());
|
||||
return obj;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue