AOP记录日志

master
niuniuniu 2023-11-23 13:34:11 +08:00
parent 6bea088b14
commit 5fd17517e4
3 changed files with 79 additions and 2 deletions

View File

@ -31,6 +31,16 @@
<version>4.12</version> <version>4.12</version>
</dependency> </dependency>
<!--AOP依赖包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.baidu.aip</groupId> <groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId> <artifactId>java-sdk</artifactId>

View File

@ -0,0 +1,64 @@
package com.dragon.vehicle.company.server.aop;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.aspectj.lang.ProceedingJoinPoint;
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;
import java.time.LocalDateTime;
/**
*
*
* @author HuZhiYong
* @version 2023/11/23 - 11:06
*/
@Aspect
@Component
public class AopLog {
private Logger logger = LoggerFactory.getLogger(this.getClass());
ThreadLocal<Long> startTime=new ThreadLocal<>();
/**
*
*/
@Pointcut("execution(public * com.dragon.vehicle.company.server.controller.*.*(..))")
public void pointcut(){
}
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
startTime.set(System.currentTimeMillis());
//使用ServletRequestAttributes请求上下文获取方法更多
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String className = joinPoint.getSignature().getDeclaringTypeName();
String methodName = joinPoint.getSignature().getName();
LocalDateTime start = LocalDateTime.now();
//使用数组获取参数
Object[] array = joinPoint.getArgs();
ObjectMapper mapper = new ObjectMapper();
//执行函数前打印日志
logger.info("开始时间:"+start);
logger.info("调用前:目标方法的类名:{},方法名:{},传递的参数为:{}",className,methodName,mapper.writeValueAsString(array));
logger.info("URL:{}",request.getRequestURL().toString());
logger.info("IP地址{}",request.getRemoteAddr());
//调用整个目标函数执行
Object obj = joinPoint.proceed();
//执行函数后打印日志
logger.info("调用后:目标方法的类名:{},方法名:{},传递的参数为:{}",className,methodName,mapper.writeValueAsString(obj));
logger.info("耗时:{}ms",System.currentTimeMillis()-startTime.get());
return obj;
}
}

View File

@ -9,6 +9,7 @@ import com.dragon.vehicle.company.common.domain.req.FirmQueryReq;
import com.dragon.vehicle.company.common.domain.req.FrimUpdateReq; import com.dragon.vehicle.company.common.domain.req.FrimUpdateReq;
import com.dragon.vehicle.company.server.service.FirmService; import com.dragon.vehicle.company.server.service.FirmService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -18,7 +19,8 @@ public class FirmController {
@Autowired @Autowired
private FirmService firmService; private FirmService firmService;
@Autowired
private RedisTemplate redisTemplate;
/** /**
* *
@ -45,7 +47,7 @@ public class FirmController {
/** /**
* *
* @return * @return
*/ */
@GetMapping("/list") @GetMapping("/list")
@ -55,6 +57,7 @@ public class FirmController {
} }
/** /**
* *
* @param firmId * @param firmId