log日志记录

This commit is contained in:
chen-xin-zhi 2025-05-03 15:03:36 +08:00
parent bce338ee19
commit 110e73b993
3 changed files with 17 additions and 19 deletions

View File

@ -4,11 +4,11 @@ import java.lang.annotation.*;
/** /**
* 自定义注解记录系统操作日志 * 自定义注解记录系统操作日志
*/ */
//Target注解决定 MyLog 注解可以加在哪些成分上如加在类身上或者属性身上或者方法身上等成分 //Target注解决定 SysLog 注解可以加在哪些成分上如加在类身上或者属性身上或者方法身上等成分
@Target({ ElementType.PARAMETER, ElementType.METHOD }) @Target({ ElementType.PARAMETER, ElementType.METHOD })
//Retention注解括号中的"RetentionPolicy.RUNTIME"意思是让 MyLog 这个注解的生命周期一直程序运行时都存在 //Retention注解括号中的"RetentionPolicy.RUNTIME"意思是让 SysLog 这个注解的生命周期一直程序运行时都存在
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface MyLog { public @interface SysLog {
/** /**
* 模块标题 * 模块标题
*/ */

View File

@ -2,7 +2,7 @@ package com.greenorange.promotion.aop;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.greenorange.promotion.annotation.MyLog; import com.greenorange.promotion.annotation.SysLog;
import com.greenorange.promotion.model.entity.SysOperLog; import com.greenorange.promotion.model.entity.SysOperLog;
import com.greenorange.promotion.service.log.SysOperLogService; import com.greenorange.promotion.service.log.SysOperLogService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -29,7 +29,7 @@ import java.util.Map;
*/ */
@Aspect @Aspect
@Component @Component
public class OperLogAspect { public class OperateLogAspect {
@Resource @Resource
private SysOperLogService sysOperLogService; private SysOperLogService sysOperLogService;
@ -38,8 +38,8 @@ public class OperLogAspect {
ThreadLocal<Long> startTime = new ThreadLocal<>(); ThreadLocal<Long> startTime = new ThreadLocal<>();
@Before("@annotation(myLog)") @Before("@annotation(sysLog)")
public void beforeMethod(JoinPoint joinPoint, MyLog myLog){ public void beforeMethod(JoinPoint joinPoint, SysLog sysLog){
startTime.set(System.currentTimeMillis()); startTime.set(System.currentTimeMillis());
} }
@ -49,8 +49,8 @@ public class OperLogAspect {
* @param joinPoint 切入点 * @param joinPoint 切入点
* @param result 返回结果 * @param result 返回结果
*/ */
@AfterReturning(value = "@annotation(myLog)", returning = "result") @AfterReturning(value = "@annotation(sysLog)", returning = "result")
public void saveOperLog(JoinPoint joinPoint, MyLog myLog, Object result) { public void saveOperateLog(JoinPoint joinPoint, SysLog sysLog, Object result) {
// 获取RequestAttributes // 获取RequestAttributes
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
// 从获取RequestAttributes中获取HttpServletRequest的信息 // 从获取RequestAttributes中获取HttpServletRequest的信息
@ -62,8 +62,8 @@ public class OperLogAspect {
Method method = signature.getMethod(); Method method = signature.getMethod();
// 获取操作 // 获取操作
SysOperLog sysOperLog = new SysOperLog(); SysOperLog sysOperLog = new SysOperLog();
sysOperLog.setTitle(myLog.title());//设置模块名称 sysOperLog.setTitle(sysLog.title());//设置模块名称
sysOperLog.setContent(myLog.content());//设置日志内容 sysOperLog.setContent(sysLog.content());//设置日志内容
// 将入参转换成json // 将入参转换成json
String params = argsArrayToString(joinPoint.getArgs()); String params = argsArrayToString(joinPoint.getArgs());
// 获取请求的类名 // 获取请求的类名
@ -118,10 +118,10 @@ public class OperLogAspect {
String methodName = method.getName(); String methodName = method.getName();
methodName = className + "." + methodName + "()"; methodName = className + "." + methodName + "()";
// 获取操作 // 获取操作
MyLog myLog = method.getAnnotation(MyLog.class); SysLog sysLog = method.getAnnotation(SysLog.class);
if (myLog != null) { if (sysLog != null) {
sysOperLog.setTitle(myLog.title());//设置模块名称 sysOperLog.setTitle(sysLog.title());//设置模块名称
sysOperLog.setContent(myLog.content());//设置日志内容 sysOperLog.setContent(sysLog.content());//设置日志内容
} }
// 将入参转换成json // 将入参转换成json
String params = argsArrayToString(joinPoint.getArgs()); String params = argsArrayToString(joinPoint.getArgs());
@ -181,7 +181,7 @@ public class OperLogAspect {
} }
//字符串截取 //字符串截取
public static String substring(String str, int start, int end) { public String substring(String str, int start, int end) {
if (str == null) { if (str == null) {
return null; return null;
} else { } else {
@ -216,7 +216,7 @@ public class OperLogAspect {
* 转换request 请求参数 * 转换request 请求参数
* @param paramMap request获取的参数数组 * @param paramMap request获取的参数数组
*/ */
public Map<String, String> converMap(Map<String, String[]> paramMap) { public Map<String, String> convertMap(Map<String, String[]> paramMap) {
Map<String, String> returnMap = new HashMap<>(); Map<String, String> returnMap = new HashMap<>();
for (String key : paramMap.keySet()) { for (String key : paramMap.keySet()) {
returnMap.put(key, paramMap.get(key)[0]); returnMap.put(key, paramMap.get(key)[0]);

View File

@ -8,7 +8,6 @@ import com.greenorange.promotion.common.BaseResponse;
import com.greenorange.promotion.common.ErrorCode; import com.greenorange.promotion.common.ErrorCode;
import com.greenorange.promotion.common.ResultUtils; import com.greenorange.promotion.common.ResultUtils;
import com.greenorange.promotion.constant.UserConstant; import com.greenorange.promotion.constant.UserConstant;
import com.greenorange.promotion.exception.BusinessException;
import com.greenorange.promotion.exception.ThrowUtils; import com.greenorange.promotion.exception.ThrowUtils;
import com.greenorange.promotion.model.dto.CommonBatchRequest; import com.greenorange.promotion.model.dto.CommonBatchRequest;
import com.greenorange.promotion.model.dto.CommonRequest; import com.greenorange.promotion.model.dto.CommonRequest;
@ -26,7 +25,6 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;