首页>代码>基于SpringBoot的轻量级Java快速开发平台>/X-SpringBoot/src/main/java/com/suke/czx/common/aspect/SysLogAspect.java
package com.suke.czx.common.aspect; import com.google.gson.Gson; import com.suke.czx.common.annotation.SysLog; import com.suke.czx.common.utils.HttpContextUtils; import com.suke.czx.common.utils.IPUtils; import com.suke.czx.modules.sys.entity.SysLogEntity; import com.suke.czx.modules.sys.entity.SysUserEntity; import com.suke.czx.modules.sys.service.SysLogService; import org.apache.shiro.SecurityUtils; 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; import java.util.Date; /** * 系统日志,切面处理类 * * @author czx * @email object_czx@163.com * @date 2017年3月8日 上午11:07:35 */ @Aspect @Component public class SysLogAspect { @Autowired private SysLogService sysLogService; @Pointcut("@annotation(com.suke.czx.common.annotation.SysLog)") public void logPointCut() { } @Around("logPointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { long beginTime = System.currentTimeMillis(); //执行方法 Object result = point.proceed(); //执行时长(毫秒) long time = System.currentTimeMillis() - beginTime; //保存日志 saveSysLog(point, time); return result; } private void saveSysLog(ProceedingJoinPoint joinPoint, long time) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); SysLogEntity sysLog = new SysLogEntity(); SysLog syslog = method.getAnnotation(SysLog.class); if(syslog != null){ //注解上的描述 sysLog.setOperation(syslog.value()); } //请求的方法名 String className = joinPoint.getTarget().getClass().getName(); String methodName = signature.getName(); sysLog.setMethod(className + "." + methodName + "()"); //请求的参数 Object[] args = joinPoint.getArgs(); try{ String params = new Gson().toJson(args[0]); sysLog.setParams(params); }catch (Exception e){ } //获取request HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); //设置IP地址 sysLog.setIp(IPUtils.getIpAddr(request)); //用户名 String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername(); sysLog.setUsername(username); sysLog.setTime(time); sysLog.setCreateDate(new Date()); //保存系统日志 sysLogService.save(sysLog); } }

不正经的90后程序猿 LV1
2024年6月1日
最代码-宋家辉 LV61
2022年10月15日
王泽军 LV2
2022年8月26日
taijh999 LV13
2022年6月27日
cxdxfx12 LV14
2022年6月21日
2468867327 LV10
2022年5月18日
tangjj7260 LV18
2022年4月25日
glyph LV10
2022年3月17日
xiaolin712 LV9
2022年3月8日
美丽的巨兔12138 LV7
2022年1月27日

陈小灏 LV18
2月22日
ma406805131 LV19
2024年12月8日
haomc052829 LV4
2024年12月3日
fei3655 LV6
2024年10月16日
waxy_123 LV2
2024年8月21日
chn-lei LV2
2024年7月31日
edpwyg LV14
2024年7月28日
Frank_F
2024年7月5日
暂无贡献等级
计科一班 LV7
2024年6月18日
2775645521 LV7
2024年6月16日