修改系统日志

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

View File

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

View File

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

View File

@ -1,19 +1,15 @@
package com.vehicle.trajectory.service.impl; 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.core.domain.Result;
import com.february.common.domain.RealData; import com.february.common.domain.RealData;
import com.february.common.domain.Vehicle; import com.february.common.domain.Vehicle;
import com.february.common.domain.VehicleType; import com.february.common.domain.VehicleType;
import com.february.common.redis.service.RedisService;
import com.vehicle.trajectory.mapper.TrajectoryMapper; import com.vehicle.trajectory.mapper.TrajectoryMapper;
import com.vehicle.trajectory.service.TrajectoryService; import com.vehicle.trajectory.service.TrajectoryService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@Service @Service
@ -27,27 +23,9 @@ public class TrajectoryServiceImpl implements TrajectoryService {
return Result.success(realData); return Result.success(realData);
} }
@Autowired
private RedisService redisService;
@Override @Override
public Result<List<Vehicle>> vehicleList() { 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(); //上线车辆的信息 List<Vehicle> vehicleList = mapper.vehicleList(); //上线车辆的信息
for (Vehicle vehicle : vehicleList) {
redisService.redisTemplate.opsForList().leftPush("状态为上线的车辆信息", JSONObject.toJSONString(vehicle));
}
return Result.success(vehicleList); return Result.success(vehicleList);
} }