Commit 98504df5 authored by yanzg's avatar yanzg

修改公式和计算帮助类

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