Aop日志
parent
c0f6a98fa2
commit
009b25f4d8
|
@ -68,6 +68,18 @@
|
|||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- aop 依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
<!-- 用于日志切面中,以 json 格式打印出入参 -->
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package com.february.common.domain.aspect;
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 切面
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.METHOD})
|
||||
@Documented
|
||||
public @interface WebLog {
|
||||
/**
|
||||
* 日志描述信息
|
||||
* @return
|
||||
**/
|
||||
String description() default "";
|
||||
}
|
|
@ -0,0 +1,117 @@
|
|||
package com.february.common.domain.aspect;
|
||||
|
||||
import com.alibaba.nacos.shaded.com.google.gson.Gson;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.*;
|
||||
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.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* AOP日志
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
//@Profile({"dev", "test"}) //只想在开发环境或者测试环境中使用
|
||||
public class WebLogAspect {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
|
||||
/** 换行符 */
|
||||
private static final String LINE_SEPARATOR = System.lineSeparator();
|
||||
|
||||
/** 以自定义 @WebLog 注解为切点 */
|
||||
@Pointcut("@annotation(com.february.common.domain.aspect.WebLog)")
|
||||
public void webLog() {}
|
||||
|
||||
/**
|
||||
* 在切点之前织入
|
||||
* @param joinPoint
|
||||
* @throws Throwable
|
||||
*/
|
||||
@Before("webLog()")
|
||||
public void doBefore(JoinPoint joinPoint) throws Throwable {
|
||||
// 开始打印请求日志
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
|
||||
// 获取 @WebLog 注解的描述信息
|
||||
String methodDescription = getAspectLogDescription(joinPoint);
|
||||
|
||||
// 打印请求相关参数
|
||||
logger.info("========================================== Start ==========================================");
|
||||
// 打印请求 url
|
||||
logger.info("URL : {}", request.getRequestURL().toString());
|
||||
// 打印描述信息
|
||||
logger.info("Description : {}", methodDescription);
|
||||
// 打印 Http method
|
||||
logger.info("HTTP Method : {}", request.getMethod());
|
||||
// 打印调用 controller 的全路径以及执行方法
|
||||
logger.info("Class Method : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
|
||||
// 打印请求的 IP
|
||||
logger.info("IP : {}", request.getRemoteAddr());
|
||||
// 打印请求入参
|
||||
logger.info("Request Args : {}", new Gson().toJson(joinPoint.getArgs()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 在切点之后织入
|
||||
* @throws Throwable
|
||||
*/
|
||||
@After("webLog()")
|
||||
public void doAfter() throws Throwable {
|
||||
// 接口结束后换行,方便分割查看
|
||||
logger.info("=========================================== End ===========================================" + LINE_SEPARATOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* 环绕
|
||||
* @param proceedingJoinPoint
|
||||
* @return
|
||||
* @throws Throwable
|
||||
*/
|
||||
@Around("webLog()")
|
||||
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
|
||||
long startTime = System.currentTimeMillis();
|
||||
Object result = proceedingJoinPoint.proceed();
|
||||
// 打印出参
|
||||
logger.info("Response Args : {}", new Gson().toJson(result));
|
||||
// 执行耗时
|
||||
logger.info("Time-Consuming : {} ms", System.currentTimeMillis() - startTime);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取切面注解的描述
|
||||
*
|
||||
* @param joinPoint 切点
|
||||
* @return 描述信息
|
||||
* @throws Exception
|
||||
*/
|
||||
public String getAspectLogDescription(JoinPoint joinPoint)
|
||||
throws Exception {
|
||||
String targetName = joinPoint.getTarget().getClass().getName();
|
||||
String methodName = joinPoint.getSignature().getName();
|
||||
Object[] arguments = joinPoint.getArgs();
|
||||
Class targetClass = Class.forName(targetName);
|
||||
Method[] methods = targetClass.getMethods();
|
||||
StringBuilder description = new StringBuilder("");
|
||||
for (Method method : methods) {
|
||||
if (method.getName().equals(methodName)) {
|
||||
Class[] clazzs = method.getParameterTypes();
|
||||
if (clazzs.length == arguments.length) {
|
||||
description.append(method.getAnnotation(WebLog.class).description());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return description.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -5,8 +5,9 @@ import com.february.common.core.domain.Result;
|
|||
import com.february.common.domain.RealData;
|
||||
import com.february.common.domain.Vehicle;
|
||||
import com.february.common.domain.VehicleType;
|
||||
import com.february.common.domain.aspect.WebLog;
|
||||
import com.vehicle.trajectory.service.TrajectoryService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
@ -15,7 +16,8 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
|
||||
@Log4j2
|
||||
//@Log4j2
|
||||
@Aspect
|
||||
@RestController
|
||||
@RequestMapping("/vehicle")
|
||||
public class TrajectoryController {
|
||||
|
@ -28,10 +30,11 @@ public class TrajectoryController {
|
|||
* 实时数据
|
||||
*/
|
||||
@GetMapping("/realDateList")
|
||||
@WebLog(description = "实时数据查看")
|
||||
public Result<List<RealData>> realDateList() {
|
||||
log.info("功能名称:【实时数据查看】,请求路径:【{}】,请求方式:【{}】", request.getRequestURI(), request.getMethod());
|
||||
//log.info("功能名称:【实时数据查看】,请求路径:【{}】,请求方式:【{}】", request.getRequestURI(), request.getMethod());
|
||||
Result<List<RealData>> result = trajectoryService.realDateList();
|
||||
log.info("请求结果:【{}】", result);
|
||||
//log.info("请求结果:【{}】", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -41,10 +44,11 @@ public class TrajectoryController {
|
|||
* 车辆信息
|
||||
*/
|
||||
@GetMapping("/vehicleList")
|
||||
@WebLog(description = "查看在线车辆")
|
||||
public Result<List<Vehicle>> carList() {
|
||||
log.info("功能名称:【查看在线车辆】,请求路径:【{}】,请求方式:【{}】", request.getRequestURI(), request.getMethod());
|
||||
//log.info("功能名称:【查看在线车辆】,请求路径:【{}】,请求方式:【{}】", request.getRequestURI(), request.getMethod());
|
||||
Result<List<Vehicle>> result = trajectoryService.vehicleList();
|
||||
log.info("请求结果:【{}】", result);
|
||||
//log.info("请求结果:【{}】", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -52,10 +56,13 @@ public class TrajectoryController {
|
|||
* 车辆类型
|
||||
*/
|
||||
@GetMapping("/vehicleTypeList")
|
||||
@WebLog(description = "车辆类型")
|
||||
public Result<List<VehicleType>> vehicleTypeList(){
|
||||
log.info("功能名称:【车辆类型】,请求路径:【{}】,请求方式:【{}】",request.getRequestURI(),request.getMethod());
|
||||
|
||||
// log.info("功能名称:【车辆类型】,请求路径:【{}】,请求方式:【{}】",request.getRequestURI(),request.getMethod());
|
||||
Result<List<VehicleType>> result = trajectoryService.vehicleTypeList();
|
||||
log.info("请求结果:【{}】",result);
|
||||
// log.info("请求结果:【{}】",result);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,3 +23,9 @@ spring:
|
|||
# 共享配置
|
||||
shared-configs:
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
# application.yml
|
||||
logging:
|
||||
level:
|
||||
com.vehicle.trajectory.controller: INFO
|
||||
pattern:
|
||||
console: '%d{yyyy-MM-dd HH:mm:ss} - %msg%n'
|
||||
|
|
Loading…
Reference in New Issue