SysLogAspect.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package net.chenlin.dp.common.aspect;
  2. import net.chenlin.dp.common.annotation.SysLog;
  3. import net.chenlin.dp.common.utils.CommonUtils;
  4. import net.chenlin.dp.common.utils.JSONUtils;
  5. import net.chenlin.dp.common.utils.ShiroUtils;
  6. import net.chenlin.dp.common.utils.WebUtils;
  7. import net.chenlin.dp.modules.sys.dao.SysLogMapper;
  8. import net.chenlin.dp.modules.sys.entity.SysLogEntity;
  9. import net.chenlin.dp.modules.sys.entity.SysUserEntity;
  10. import org.aspectj.lang.ProceedingJoinPoint;
  11. import org.aspectj.lang.annotation.Around;
  12. import org.aspectj.lang.annotation.Aspect;
  13. import org.aspectj.lang.annotation.Pointcut;
  14. import org.aspectj.lang.reflect.MethodSignature;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Component;
  17. import java.lang.reflect.Method;
  18. /**
  19. * 系统日志,切面处理类
  20. * @author zcl<yczclcn@163.com>
  21. */
  22. @Aspect
  23. @Component
  24. public class SysLogAspect {
  25. @Autowired
  26. private SysLogMapper sysLogMapper;
  27. @Pointcut("@annotation(net.chenlin.dp.common.annotation.SysLog)")
  28. public void logPointCut() {
  29. }
  30. @Around("logPointCut()")
  31. public Object around(ProceedingJoinPoint point) throws Throwable {
  32. long beginTime = System.currentTimeMillis();
  33. //执行方法
  34. Object result = point.proceed();
  35. //执行时长(毫秒)
  36. long time = System.currentTimeMillis() - beginTime;
  37. //保存日志
  38. saveSysLog(point, time);
  39. return result;
  40. }
  41. private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
  42. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  43. Method method = signature.getMethod();
  44. SysLogEntity sysLog = new SysLogEntity();
  45. SysLog syslog = method.getAnnotation(SysLog.class);
  46. if(syslog != null){
  47. //注解上的描述
  48. sysLog.setOperation(syslog.value());
  49. }
  50. //请求的方法名
  51. String className = joinPoint.getTarget().getClass().getName();
  52. String methodName = signature.getName();
  53. sysLog.setMethod(className + "." + methodName + "()");
  54. //请求的参数
  55. Object[] args = joinPoint.getArgs();
  56. try{
  57. String params = JSONUtils.beanToJson(args[0]);
  58. sysLog.setParams(params);
  59. }catch (Exception e){
  60. }
  61. //设置IP地址
  62. sysLog.setIp(WebUtils.getIpAddr());
  63. //用户名
  64. SysUserEntity currUser = ShiroUtils.getUserEntity();
  65. if(CommonUtils.isNullOrEmpty(currUser)) {
  66. if(CommonUtils.isNullOrEmpty(sysLog.getParams())) {
  67. sysLog.setUserId(-1L);
  68. sysLog.setUsername(sysLog.getParams());
  69. } else {
  70. sysLog.setUserId(-1L);
  71. sysLog.setUsername("获取用户信息为空");
  72. }
  73. } else {
  74. sysLog.setUserId(ShiroUtils.getUserId());
  75. sysLog.setUsername(ShiroUtils.getUserEntity().getUsername());
  76. }
  77. sysLog.setTime(time);
  78. //保存系统日志
  79. sysLogMapper.save(sysLog);
  80. }
  81. }