修改系统日志

master
张小东 2023-11-22 11:30:22 +08:00
parent 77a4e2758e
commit 2f5a9ed00e
5 changed files with 118 additions and 133 deletions

View File

@ -26,12 +26,12 @@
<version>3.6.3</version>
</dependency>
<!-- RuoYi Common Log -->
<!-- <dependency>-->
<!-- <groupId>com.february</groupId>-->
<!-- <artifactId>february-common-log</artifactId>-->
<!-- <version>3.6.3</version>-->
<!-- </dependency>-->
<!--RuoYi Common Log -->
<dependency>
<groupId>com.february</groupId>
<artifactId>february-common-log</artifactId>
<version>3.6.3</version>
</dependency>
<!-- RuoYi Common DataScope -->
<dependency>
@ -85,6 +85,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,16 +1,16 @@
package com.vehicle.trajectory.aspect;
import java.lang.annotation.*;
/**
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface WebLog {
/**
*
* @return
**/
String description() default "";
}
//package com.vehicle.trajectory.aspect;
//import java.lang.annotation.*;
//
///**
// * 切面类
// */
//@Retention(RetentionPolicy.RUNTIME)
//@Target({ElementType.METHOD})
//@Documented
//public @interface WebLog {
// /**
// * 日志描述信息
// * @return
// **/
// String description() default "";
//}

View File

@ -1,87 +1,87 @@
package com.vehicle.trajectory.aspect;
import 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
public class WebLogAspect {
private final static Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
private static final Gson gson = new Gson(); // 用于重复使用Gson实例
private static final String LINE_SEPARATOR = System.lineSeparator();
@Pointcut("@annotation(com.vehicle.trajectory.aspect.WebLog)")
public void webLog() {}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) {
// 开始打印请求日志
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
// 获取 @WebLog 注解的描述信息
String methodDescription;
try {
methodDescription = getAspectLogDescription(joinPoint);
} catch (Exception e) {
methodDescription = "无法获取方法描述信息";
logger.error("获取方法描述信息时发生异常: ", e);
}
// 打印请求相关参数
logger.info("========================================== Start ==========================================");
logger.info("URL : {}", request.getRequestURL().toString());
logger.info("Description : {}", methodDescription);
logger.info("HTTP Method : {}", request.getMethod());
logger.info("Class Method : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
logger.info("IP : {}", request.getRemoteAddr());
logger.info("Request Args : {}", gson.toJson(joinPoint.getArgs()));
}
}
@After("webLog()")
public void doAfter() {
logger.info("=========================================== End ==========================================={}", LINE_SEPARATOR);
}
@Around("webLog()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object result = proceedingJoinPoint.proceed();
logger.info("Response Args : {}", gson.toJson(result));
logger.info("Time-Consuming : {} ms", System.currentTimeMillis() - startTime);
return result;
}
private String getAspectLogDescription(JoinPoint joinPoint) throws ClassNotFoundException {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Class<?> targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
for (Method method : methods) {
if (method.getName().equals(methodName) && method.getParameterTypes().length == arguments.length) {
WebLog webLog = method.getAnnotation(WebLog.class);
if (webLog != null) {
return webLog.description();
}
}
}
return "";
}
}
//package com.vehicle.trajectory.aspect;
//
//import 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
//public class WebLogAspect {
//
// private final static Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
// private static final Gson gson = new Gson(); // 用于重复使用Gson实例
// private static final String LINE_SEPARATOR = System.lineSeparator();
//
// @Pointcut("@annotation(com.vehicle.trajectory.aspect.WebLog)")
// public void webLog() {}
//
// @Before("webLog()")
// public void doBefore(JoinPoint joinPoint) {
// // 开始打印请求日志
// ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
// if (attributes != null) {
// HttpServletRequest request = attributes.getRequest();
//
// // 获取 @WebLog 注解的描述信息
// String methodDescription;
// try {
// methodDescription = getAspectLogDescription(joinPoint);
// } catch (Exception e) {
// methodDescription = "无法获取方法描述信息";
// logger.error("获取方法描述信息时发生异常: ", e);
// }
//
// // 打印请求相关参数
// logger.info("========================================== Start ==========================================");
// logger.info("URL : {}", request.getRequestURL().toString());
// logger.info("Description : {}", methodDescription);
// logger.info("HTTP Method : {}", request.getMethod());
// logger.info("Class Method : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
// logger.info("IP : {}", request.getRemoteAddr());
// logger.info("Request Args : {}", gson.toJson(joinPoint.getArgs()));
// }
// }
//
// @After("webLog()")
// public void doAfter() {
// logger.info("=========================================== End ==========================================={}", LINE_SEPARATOR);
// }
//
// @Around("webLog()")
// public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
// long startTime = System.currentTimeMillis();
// Object result = proceedingJoinPoint.proceed();
// logger.info("Response Args : {}", gson.toJson(result));
// logger.info("Time-Consuming : {} ms", System.currentTimeMillis() - startTime);
// return result;
// }
//
// private String getAspectLogDescription(JoinPoint joinPoint) throws ClassNotFoundException {
// String targetName = joinPoint.getTarget().getClass().getName();
// String methodName = joinPoint.getSignature().getName();
// Object[] arguments = joinPoint.getArgs();
// Class<?> targetClass = Class.forName(targetName);
// Method[] methods = targetClass.getMethods();
// for (Method method : methods) {
// if (method.getName().equals(methodName) && method.getParameterTypes().length == arguments.length) {
// WebLog webLog = method.getAnnotation(WebLog.class);
// if (webLog != null) {
// return webLog.description();
// }
// }
// }
// return "";
// }
//}

View File

@ -5,11 +5,10 @@ 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.vehicle.trajectory.aspect.WebLog;
import com.february.common.log.aspect.WebLog;
import com.vehicle.trajectory.service.TrajectoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

View File

@ -1,19 +1,15 @@
package com.vehicle.trajectory.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.redis.service.RedisService;
import com.vehicle.trajectory.mapper.TrajectoryMapper;
import com.vehicle.trajectory.service.TrajectoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
@ -27,27 +23,9 @@ public class TrajectoryServiceImpl implements TrajectoryService {
return Result.success(realData);
}
@Autowired
private RedisService redisService;
@Override
public Result<List<Vehicle>> vehicleList() {
Boolean aBoolean = redisService.hasKey("状态为上线的车辆信息");//查询redis中是否有此键
if (Boolean.TRUE.equals(aBoolean)){
List<Object> list = redisService.redisTemplate.opsForList().range("状态为上线的车辆信息", 0, -1);
ArrayList<Vehicle> carArrayList = new ArrayList<>();
if (list != null) {
for (Object o : list) {
String o1 = (String) o;
Vehicle vehicle = JSON.parseObject(o1, Vehicle.class);
carArrayList.add(vehicle);
}
return Result.success(carArrayList);
}
}
List<Vehicle> vehicleList = mapper.vehicleList(); //上线车辆的信息
for (Vehicle vehicle : vehicleList) {
redisService.redisTemplate.opsForList().leftPush("状态为上线的车辆信息", JSONObject.toJSONString(vehicle));
}
return Result.success(vehicleList);
}