1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
| package com.scaffolding.demo.config;
import com.alibaba.fastjson.JSON; import com.scaffolding.demo.annotation.Log; import com.scaffolding.demo.common.BaseController; import com.scaffolding.demo.sys.model.ExceptionLog; import com.scaffolding.demo.sys.model.OperationLog; import com.scaffolding.demo.sys.service.ExceptionLogService; import com.scaffolding.demo.sys.service.OperationLogService; import com.scaffolding.demo.sys.service.UserService; import com.scaffolding.demo.utils.IPUtil; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; 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.beans.factory.annotation.Value; import org.springframework.core.DefaultParameterNameDiscoverer; import org.springframework.core.ParameterNameDiscoverer; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder;
import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.time.LocalDateTime; import java.util.HashMap; import java.util.Map; import java.util.Objects;
@Slf4j @Aspect @Component public class OperationLogAspect extends BaseController {
@Value("${app.version}") private String version; @Autowired private OperationLogService operationLogService; @Autowired private ExceptionLogService exceptionLogService; @Autowired private UserService userService;
@Pointcut("@annotation(com.scaffolding.demo.annotation.Log)") public void operationLogPointCut() { }
@Pointcut("execution(* com.scaffolding.demo.sys.controller..*.*(..))") public void operationExceptionLogPointCut() { }
@AfterReturning(value = "operationLogPointCut()", returning = "results") public void saveOperationLog(JoinPoint joinPoint, Object results) { log.info("开始执行拦截用户操作日志"); RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); HttpServletRequest request = (HttpServletRequest) Objects.requireNonNull(requestAttributes) .resolveReference(RequestAttributes.REFERENCE_REQUEST);
OperationLog operationLog = new OperationLog(); try { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); Log log = method.getAnnotation(Log.class); if (log != null) { operationLog.setModule(log.operationModule()); operationLog.setType(log.operationType()); operationLog.setDescription(log.operationDesc()); } String className = joinPoint.getTarget().getClass().getName(); String methodName = method.getName(); operationLog.setMethod(className + "." + methodName); Map<String, Object> rtnMap = convertMap(joinPoint, method); String params = JSON.toJSONString(rtnMap); operationLog.setRequestParam(params); operationLog.setResponseParam(JSON.toJSONString(results)); operationLog.setUserId(getUserId()); operationLog.setUserName(userService.getById(getUserId()).getNickName()); operationLog.setIp(IPUtil.getRequestIpAddress(request)); operationLog.setUri(request.getRequestURI()); operationLog.setCreateTime(LocalDateTime.now()); operationLog.setVersion(version); operationLogService.save(operationLog); } catch (Exception e) { log.error("拦截用户操作日志异常"); e.printStackTrace(); } }
@AfterThrowing(pointcut = "operationExceptionLogPointCut()", throwing = "exception") public void saveExceptionLog(JoinPoint joinPoint, Throwable exception) { log.info("开始拦截异常日志信息"); RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); HttpServletRequest request = (HttpServletRequest) Objects.requireNonNull(requestAttributes) .resolveReference(RequestAttributes.REFERENCE_REQUEST);
ExceptionLog exceptionLog = new ExceptionLog(); try { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); String className = joinPoint.getTarget().getClass().getName(); String methodName = method.getName(); Map<String, Object> rtnMap = convertMap(joinPoint, method); String params = JSON.toJSONString(rtnMap);
exceptionLog.setRequestParam(params); exceptionLog.setMethod(className + "." + methodName); exceptionLog.setName(exception.getClass().getName()); exceptionLog.setMessage(stackTraceToString(exception.getClass().getName(), exception.getMessage(), exception.getStackTrace())); exceptionLog.setUserId(getUserId()); exceptionLog.setUserName(userService.getById(getUserId()).getNickName()); exceptionLog.setUri(request.getRequestURI()); exceptionLog.setIp(IPUtil.getRequestIpAddress(request)); exceptionLog.setVersion(version); exceptionLog.setCreateTime(LocalDateTime.now());
exceptionLogService.save(exceptionLog); } catch (Exception e) { log.error("拦截异常日志信息发生异常"); e.printStackTrace(); } }
private Map<String, Object> convertMap(JoinPoint joinPoint, Method method) { Object[] args = joinPoint.getArgs(); ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer(); String[] parameterNames = parameterNameDiscoverer.getParameterNames(method); Map<String, Object> paramMap = new HashMap<>(); for (int i = 0; i < Objects.requireNonNull(parameterNames).length; i++) { paramMap.put(parameterNames[i], args[i].toString()); } return paramMap; }
private String stackTraceToString(String exceptionName, String exceptionMessage, StackTraceElement[] elements) { StringBuffer stringBuffer = new StringBuffer(); for (StackTraceElement stet : elements) { stringBuffer.append(stet + "\n"); } return exceptionName + ":" + exceptionMessage + "\n\t" + stringBuffer.toString(); } }
|