1 1 package com.idoipo.infras.gateway.open.config; 2 2 3 3 import com.alibaba.fastjson.JSON; 4 4 import com.alibaba.fastjson.JSONObject; 5 5 import com.idoipo.infras.gateway.open.model.InvokeLogModel; 6 6 import com.idoipo.infras.gateway.open.service.IInvokeLogService; 7 7 import org.aspectj.lang.ProceedingJoinPoint; 8 8 import org.aspectj.lang.annotation.Around; 9 9 import org.aspectj.lang.annotation.Aspect; 10 10 import org.aspectj.lang.annotation.Pointcut; 11 11 import org.slf4j.Logger; 12 12 import org.slf4j.LoggerFactory; 13 13 import org.springframework.beans.factory.annotation.Autowired; 14 14 import org.springframework.context.annotation.Configuration; 15 15 import org.springframework.web.context.request.RequestAttributes; 16 16 import org.springframework.web.context.request.RequestContextHolder; 17 17 import org.springframework.web.context.request.ServletRequestAttributes; 18 18 19 19 import javax.servlet.http.HttpServletRequest; 20 20 import java.lang.reflect.Field; 21 21 import java.util.Date; 22 22 import java.util.HashMap; 23 23 import java.util.Map; 24 24 25 25 /** 26 26 * Create by liping on 2018/8/20 27 27 */ 28 28 @Aspect 29 29 @Configuration//定义一个切面 30 30 public class LogRecodeAspect { 31 31 32 32 private static final Logger logger = LoggerFactory.getLogger(LogRecodeAspect.class); 33 33 34 34 @Autowired 35 35 IInvokeLogService invokeLogService; 36 36 37 37 // 定义切点Pointcut 38 38 @Pointcut("execution(public * com.idoipo.infras.gateway.open.controller..*.*(..))") 39 39 public void excudeService() { 40 40 } 41 41 42 42 @Around("excudeService()") 43 43 public Object doAround(ProceedingJoinPoint pjp) throws Throwable { 44 44 RequestAttributes ra = RequestContextHolder.getRequestAttributes(); 45 45 ServletRequestAttributes sra = (ServletRequestAttributes) ra; 46 46 HttpServletRequest request = sra.getRequest(); 47 47 String url = request.getRequestURI(); 48 48 String method = request.getMethod(); 49 49 String queryString = request.getQueryString(); 50 50 Object[] args = pjp.getArgs(); 51 51 String params = ""; 52 52 String invokeUser = ""; 53 53 int userFlag = 1; 54 54 //获取请求参数集合并进行遍历拼接 55 55 if(args.length>0){ 56 56 if("POST".equals(method)){ 57 57 Object object = args[0]; 58 58 Map map = getKeyAndValue(object); 69 params = JSON.toJSONString(map); 59 70 }else if("GET".equals(method)){ 60 71 if(null!=queryString&&""!=queryString){ 61 72 String[] paramArray = queryString.split("&"); 62 73 for(String param : paramArray){ 63 74 String[] keyValue = param.split("="); 64 75 String key = keyValue[0]; 65 76 if(keyValue[0].equals("user")){ 66 77 invokeUser = keyValue[1]; 67 78 }else if(keyValue[0].equals("userFlag")) 68 79 userFlag = Integer.parseInt(keyValue[1]); 69 80 } 70 81 params = queryString; 71 82 } 72 83 73 84 } 74 85 } 75 86 logger.info("请求开始地址={},类型={},参数={}:",url,method,params); 76 87 Date startTime = new Date(); 77 88 // result的值就是被拦截方法的返回值 78 89 Object result = pjp.proceed(); 79 90 boolean responseResult = false; 80 91 String response = JSONObject.toJSONString(result); 81102 logger.info("请求结束===返回值={}:" + response); 82103 Date endTime = new Date(); 83104 InvokeLogModel invokeLogModel = new InvokeLogModel(); 84105 invokeLogModel.setInterfaceName(url); 85106 invokeLogModel.setInterfaceMethod(method); 86107 87108 invokeLogModel.setInvokeStartTime(startTime); 88109 invokeLogModel.setRequestParam(params); 89110 90111 invokeLogModel.setResponseResult(responseResult); 91112 invokeLogModel.setInvokeEndTime(endTime);115 invokeLogService.insertInvokerLog(invokeLogModel); 92116 return result; 93117 } 94118 95119 public static Map<String, Object> getKeyAndValue(Object obj) { 96120 Map<String, Object> map = new HashMap<>(); 97121 // 得到类对象 98122 Class userCla = obj.getClass(); 99123 /* 得到类中的所有属性集合 */ 100124 Field[] fs = userCla.getDeclaredFields(); 101125 for (int i = 0; i < fs.length; i++) { 102126 Field f = fs[i]; 103127 f.setAccessible(true); // 设置些属性是可以访问的 104128 Object val ; 105129 try { 106130 val = f.get(obj); 107131 // 得到此属性的值 108132 map.put(f.getName(), val);// 设置键值 109133 } catch (IllegalArgumentException e) { 110134 logger.error("解析参数异常",e); 111135 } catch (IllegalAccessException e) { 112136 logger.error("解析参数异常",e); 113137 } 114138 115139 } 116140 return map; 117141 } 118142 } 119 120package com.idoipo.infras.gateway.open.model; 121 122import java.util.Date; 123 124public class InvokeLogModel { 125 126 //自增id 127 private int id; 128 //接口名 129 private String interfaceName; 130 //接口方法类型 GET or POST之类的 131 private String interfaceMethod; 132 //调用接口开始的时间 133 private Date invokeStartTime; 134 //调用接口结束的时间 135 private Date invokeEndTime; 136 //接口请求参数 137 private String requestParam; 138 //是否有响应值 139 private Boolean responseResult; 140 141 public int getId() { 142 return id; 143 } 144 145 public void setId(int id) { 146 this.id = id; 147 } 148 149 150 public String getInterfaceName() { 151 return interfaceName; 152 } 153 154 public void setInterfaceName(String interfaceName) { 155 this.interfaceName = interfaceName; 156 } 157 158 public String getInterfaceMethod() { 159 return interfaceMethod; 160 } 161 162 public void setInterfaceMethod(String interfaceMethod) { 163 this.interfaceMethod = interfaceMethod; 164 } 165 166 public Date getInvokeStartTime() { 167 return invokeStartTime; 168 } 169 170 public void setInvokeStartTime(Date invokeStartTime) { 171 this.invokeStartTime = invokeStartTime; 172 } 173 174 public Date getInvokeEndTime() { 175 return invokeEndTime; 176 } 177 178 public void setInvokeEndTime(Date invokeEndTime) { 179 this.invokeEndTime = invokeEndTime; 180 } 181 182 public String getRequestParam() { 183 return requestParam; 184 } 185 186 public void setRequestParam(String requestParam) { 187 this.requestParam = requestParam; 188 } 189 190 public Boolean getResponseResult() { 191 return responseResult; 192 } 193 194 public void setResponseResult(Boolean responseResult) { 195 this.responseResult = responseResult; 196 } 197 198 @Override 199 public String toString() { 200 return "InvokeLogModel{" + 201 "id=" + id + 202 ", interfaceName='" + interfaceName + '\'' + 203 ", interfaceMethod='" + interfaceMethod + '\'' + 204 ", invokeStartTime=" + invokeStartTime + 205 ", invokeEndTiem=" + invokeEndTime + 206 ", requestParam='" + requestParam + '\'' + 207 ", responseResult=" + responseResult + 208 '}'; 209 } 210}
springcloud 定义切面实现对请求操作记录日志,方便后面分析接口详情
Easter79
2021-10-12
1026 1 0
点赞
收藏
评论区
加载中...