From ed8c7a5effd0d423ce1118b680ecdca6fe732609 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期三, 02 七月 2025 16:43:13 +0800 Subject: [PATCH] Merge branch 'master' of http://192.168.11.205:9000/r/P2022036_Service --- src/main/java/com/lf/server/aspect/LogAspect.java | 151 +++++++++++++++++++++++++++++++++++++------------ 1 files changed, 113 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/lf/server/aspect/LogAspect.java b/src/main/java/com/lf/server/aspect/LogAspect.java index cfc6e11..590876c 100644 --- a/src/main/java/com/lf/server/aspect/LogAspect.java +++ b/src/main/java/com/lf/server/aspect/LogAspect.java @@ -1,12 +1,23 @@ package com.lf.server.aspect; +import com.lf.server.annotation.SysLog; +import com.lf.server.entity.sys.OperateEntity; +import com.lf.server.entity.sys.UserEntity; +import com.lf.server.helper.WebHelper; +import com.lf.server.service.sys.OperateService; +import com.lf.server.service.sys.TokenService; +import io.swagger.annotations.Api; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; /** @@ -16,63 +27,127 @@ @Aspect @Component public class LogAspect { - @Pointcut("") - public void logPointCut() { + @Autowired + private TokenService tokenService; + @Autowired + private OperateService operateService; + + private final static String SELECT = "select"; + + private final static String INSERT = "insert"; + + private final static String UPDATE = "update"; + + private final static String DELETE = "delete"; + + private final static String UPLOAD = "upload"; + + private final static String DOWNLOAD = "download"; + + private final static String COUNT = "count"; + + private final static String SPLIT = "\\\\"; + + private static final Log log = LogFactory.getLog(LogAspect.class); + + @Pointcut("@annotation(com.lf.server.annotation.SysLog)") + public void logPointCut() { + // } + /** + * 鐜粫閫氱煡 @Around锛屼篃鍙互浣跨敤 @Before (鍓嶇疆閫氱煡) 鎴� @After (鍚庣疆閫氱煡) + */ @Around("logPointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { long beginTime = System.currentTimeMillis(); + // 鎵ц鏂规硶 Object result = point.proceed(); // 鎵ц鏃堕暱(姣) long time = System.currentTimeMillis() - beginTime; - // System.out.println("time:"+time) // 淇濆瓨鏃ュ織 - saveLogAction(point, time); + saveLog(point, time); return result; } - private void saveLogAction(ProceedingJoinPoint joinPoint, long time) { - MethodSignature signature = (MethodSignature) joinPoint.getSignature(); - Method method = signature.getMethod(); - /*LogAction logAction = new LogAction(); - com.landtool.lanbase.common.annotation.LogAction log = method.getAnnotation(com.landtool.lanbase.common.annotation.LogAction.class); - if(log != null){ - // 娉ㄨВ涓婄殑鎻忚堪 - String[] list = log.value().split(","); - logAction.setLargemodel(list[0]); //澶фā鍧� - logAction.setSmallmodel(list[1]);//灏忔ā鍧� - logAction.setRemark(list[2]); //澶囨敞 - logAction.setActiontype(list[3]);//鎿嶄綔绫诲瀷 + /** + * 淇濆瓨鏃ュ織 + */ + private void saveLog(ProceedingJoinPoint joinPoint, long time) { + try { + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + Class<?> clazz = joinPoint.getTarget().getClass(); + Method method = signature.getMethod(); - //logAction.setRemark(log.value()); - //logAction.setActiontype(log.value()); + HttpServletRequest req = WebHelper.getRequest(); + String url = req.getServletPath(); + + OperateEntity oe = new OperateEntity(); + oe.setIp(WebHelper.getIpAddress(req)); + oe.setUrl(url); + oe.setExec(time); + + // 璁剧疆妯″潡 + Api api = clazz.getAnnotation(Api.class); + if (api != null) { + String[] strs = api.tags()[0].split(SPLIT); + oe.setModular1(strs[0]); + oe.setModular2(strs[1]); + } + + // 璁剧疆绫诲悕\鏂规硶鍚嶃�佺被鍒� + oe.setClazz(clazz.getName() + "." + method.getName()); + oe.setType(getType(method.getName())); + + // 璁剧疆澶囨敞 + SysLog sysLog = method.getAnnotation(SysLog.class); + if (sysLog != null) { + oe.setBak(sysLog.value()); + } + + UserEntity ue = tokenService.getCurrentUser(req); + if (ue != null) { + oe.setUserid(ue.getId()); + } + + operateService.insertOperate(oe); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + } + + /** + * 鑾峰彇鎿嶄綔绫诲瀷 + */ + private static int getType(String methodName) { + if (methodName.contains(SELECT)) { + return 1; + } + if (methodName.contains(INSERT)) { + return 2; + } + if (methodName.contains(UPDATE)) { + return 3; + } + if (methodName.contains(DELETE)) { + return 4; + } + if (methodName.contains(UPLOAD)) { + return 5; + } + if (methodName.contains(DOWNLOAD)) { + return 6; + } + // methodName.indexOf(COUNT) > -1 + if (methodName.contains(COUNT)) { + return 7; } - //鑾峰彇request - HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); - String url=request.getServletPath(); - logAction.setRequesturl(url); - //璁剧疆IP鍦板潃 - logAction.setRequestip(IPUtils.getIpAddr(request)); - - //鐢ㄦ埛鍚� - Long userid = ((OrgUser) SecurityUtils.getSubject().getPrincipal()).getUserid(); - logAction.setUserid(userid); - - logAction.setAppid(loginConfig.getAppId()); - - //logAction.setLargemodel(loginConfig.getAppFullName()); - - - - - //淇濆瓨绯荤粺鏃ュ織 - logActionService.save(logAction);*/ + return 0; } } -- Gitblit v1.9.3