Commit 98504df5 authored by yanzg's avatar yanzg

修改公式和计算帮助类

parent 84c1824b
......@@ -9,6 +9,7 @@ import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.LogVo;
import com.yanzuoguang.util.vo.ResponseResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;
......@@ -16,6 +17,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.channels.Channel;
import java.util.ArrayList;
import java.util.List;
......@@ -49,7 +51,19 @@ public class BaseRequestAspect {
private String filter;
@Autowired
private LogBase logBase;
private LogLocal logLocal;
/**
* 开始记录日志对象
*
* @param actionKey
* @param url
* @param body
* @return
*/
protected LogVo startLog(String actionKey, String url, String body) {
return this.logLocal.startLog(actionKey, String.format("%s:%s", this.applicationName, url), body);
}
/**
* 初始化日志Vo
......@@ -59,7 +73,7 @@ public class BaseRequestAspect {
* @param responseResult 返回参数
* @return
*/
protected LogVo initLogInterVo(String url, ProceedingJoinPoint joinPoint, ResponseResult responseResult) {
protected LogVo abc(String url, ProceedingJoinPoint joinPoint, ResponseResult responseResult) {
LogVo logInterVo = new LogVo();
logInterVo.setLogId(StringHelper.getNewID());
//平台名
......@@ -75,6 +89,20 @@ public class BaseRequestAspect {
return logInterVo;
}
/**
* 获取请求内容
*
* @param joinPoint
* @return
*/
protected String getRequestBody(ProceedingJoinPoint joinPoint) {
Object firstParameter = getFirstDataParameter(joinPoint.getArgs());
if (firstParameter instanceof String) {
return (String) firstParameter;
}
return JsonHelper.serialize(firstParameter);
}
/**
* 获取请求的参数
*
......@@ -82,6 +110,7 @@ public class BaseRequestAspect {
* @return
*/
protected Object getFirstDataParameter(Object[] args) {
// 获取请求参数
List<Object> para = getDataParameters(args);
Object paraTo = para;
if (para.size() == 1) {
......@@ -99,7 +128,13 @@ public class BaseRequestAspect {
protected List<Object> getDataParameters(Object[] args) {
List<Object> para = new ArrayList<>();
for (Object item : args) {
if (item instanceof HttpServlet || item instanceof HttpServletResponse || item instanceof HttpServletRequest || item instanceof MultipartFile) {
if (item instanceof HttpServlet
|| item instanceof HttpServletResponse
|| item instanceof HttpServletRequest
|| item instanceof MultipartFile
|| item instanceof Message
|| item instanceof Channel
) {
continue;
}
para.add(item);
......@@ -158,18 +193,14 @@ public class BaseRequestAspect {
/**
* 保存日志
*
* @param url
* @param joinPoint
* @param result
* @param resultEx
* @param start
*/
protected void responseLog(boolean save, boolean clear, String tag, String url, ProceedingJoinPoint joinPoint, long start, Object result, Exception resultEx) {
protected void responseLog(LogVo log, boolean clear, String tag, ProceedingJoinPoint joinPoint, long start, Object result, Exception resultEx) {
try {
String name = joinPoint.getSignature().getName();
if (StringHelper.isEmpty(url)) {
url = String.format("%s.%s", joinPoint.getSignature().getDeclaringTypeName(), name);
}
// 处理结果
ResponseResult responseResult = responseDefault;
if (result instanceof ResponseResult) {
......@@ -190,19 +221,12 @@ public class BaseRequestAspect {
}
// 日志请求不记录,防止死循环递归
boolean isLog = isLog(name, url);
boolean isLog = isLog(name, log.getActionSubKey());
if (isLog) {
return;
}
// 正常请求不记录
boolean error = resultEx != null || responseResult == null || !ResultConstants.SUCCESS.equals(responseResult.getCode());
boolean logFlag = (logAll && save) || error;
if (logFlag) {
LogVo logVo = initLogInterVo(url, joinPoint, responseResult);
logVo.setUseTime((int) time);
logBase.addLog(logVo);
}
logLocal.result(log, responseResult.getCode(), JsonHelper.serialize(responseResult));
} catch (Exception e) {
e.printStackTrace();
} finally {
......@@ -228,4 +252,15 @@ public class BaseRequestAspect {
boolean isLog = applicationName.matches(filter) || name.matches(filter) || url.matches(filter);
return isLog;
}
/**
* 获取方法名称
*
* @return
*/
protected String getMethodUrl(ProceedingJoinPoint joinPoint) {
String name = joinPoint.getSignature().getName();
return name;
}
}
......@@ -3,13 +3,12 @@ package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.util.contants.ResultConstants;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.vo.LogVo;
import com.yanzuoguang.util.vo.ResponseResult;
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.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
......@@ -23,10 +22,6 @@ public class FeignAspect extends BaseRequestAspect {
private static final String TAG = FeignAspect.class.getSimpleName();
@Value("${yzg.logFeign:false}")
private boolean logFeign;
/**
* AOP的表达式
*/
......@@ -44,6 +39,7 @@ public class FeignAspect extends BaseRequestAspect {
public Object requestFeignAround(ProceedingJoinPoint joinPoint) throws Throwable {
boolean clear = requestLogInit();
long start = requestLog(TAG, joinPoint);
LogVo log = startLog(TAG, getMethodUrl(joinPoint), getRequestBody(joinPoint));
Object result = null;
Exception ex = null;
......@@ -67,7 +63,7 @@ public class FeignAspect extends BaseRequestAspect {
ex = e;
throw e;
} finally {
responseLog(logFeign, clear, TAG, StringHelper.EMPTY, joinPoint, start, result, ex);
responseLog(log, clear, TAG, joinPoint, start, result, ex);
}
}
......
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.LogVo;
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.Value;
import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
......@@ -26,9 +25,6 @@ public class MqAspect extends BaseRequestAspect {
private static final String TAG = MqAspect.class.getSimpleName();
@Value("${yzg.logMq:false}")
private boolean logMq;
/**
* exec aop point aspect
*/
......@@ -51,6 +47,7 @@ public class MqAspect extends BaseRequestAspect {
clear = requestLogInit();
}
long start = requestLog(TAG, joinPoint);
LogVo log = startLog(TAG, getMethodUrl(joinPoint), getRequestBody(joinPoint));
Object result = null;
Exception ex = null;
try {
......@@ -62,7 +59,7 @@ public class MqAspect extends BaseRequestAspect {
ex = e;
throw e;
} finally {
responseLog(logMq, clear, TAG, StringHelper.EMPTY, joinPoint, start, result, ex);
responseLog(log, clear, TAG, joinPoint, start, result, ex);
}
}
......
......@@ -2,11 +2,11 @@ package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.cloud.service.TokenServiceCall;
import com.yanzuoguang.token.TokenHelper;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.TypeHelper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.LogVo;
import com.yanzuoguang.util.vo.ResponseResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
......@@ -20,7 +20,6 @@ import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
/**
......@@ -38,9 +37,6 @@ public class WebAspect extends BaseRequestAspect {
@Autowired
private TokenServiceCall tokenServiceCall;
@Value("${yzg.logWeb:false}")
private boolean logWeb;
@Value("${yzg.gateway:^.*gateway.*$}")
private String gateWay;
......@@ -77,6 +73,7 @@ public class WebAspect extends BaseRequestAspect {
boolean clear = requestLogInit();
// 用户数据库记录
long start = requestLog(TAG, joinPoint);
LogVo log = startLog(TAG, HttpAspectUtil.getHttpRequestUrl(), getRequestBody(joinPoint));
Exception ex = null;
boolean isInit = false;
try {
......@@ -118,7 +115,7 @@ public class WebAspect extends BaseRequestAspect {
if (isInit) {
TokenHelper.remove();
}
responseLog(logWeb, clear, TAG, HttpAspectUtil.getHttpRequestUrl(), joinPoint, start, result, ex);
responseLog(log, clear, TAG, joinPoint, start, result, ex);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment