Commit e12dc963 authored by yanzg's avatar yanzg

修复bug

parent b57fe37c
......@@ -18,55 +18,55 @@ public class LogVo extends BaseVo {
* 日志id
*/
@ApiModelProperty(value = "日志Id", required = true, example = "{{$uuid}}")
private String logId;
private volatile String logId;
/**
* 工程实例名
*/
@ApiModelProperty(value = "项目名称", required = true, example = "MQ|WEB|FEIGN")
private String actionKey;
private volatile String actionKey;
/**
* 请求接口地址
*/
@ApiModelProperty(value = "接口地址", required = true, example = "tbd-order/order/save")
private String actionSubKey;
private volatile String actionSubKey;
/**
* 请求内容
*/
@ApiModelProperty(value = "请求内容", required = true, example = "{}")
private String content;
private volatile String content;
/**
* 执行后的请求内容
*/
@ApiModelProperty(value = "执行后的请求内容,仅仅在内容有变化时写入", required = true, example = "{}")
private String contentTo;
private volatile String contentTo;
/**
* 返回参数
*/
@ApiModelProperty(value = "返回结果", required = true, example = "{}")
private String result;
private volatile String result;
/**
* 接口处理状态,是否有异常
*/
@ApiModelProperty(value = "执行状态", notes = "0表示成功,其他状态表示失败", required = true, example = "0")
private String status;
private volatile String status;
/**
* 使用时间
*/
@ApiModelProperty(value = "执行耗时", notes = "单位(毫秒)", required = true, example = "20")
private int useTime;
private volatile int useTime;
/**
* 创建时间
*/
@ApiModelProperty(value = "创建时间", notes = "单位(毫秒)", required = false, example = "1987-11-24 23:15:18")
private String createDate;
private volatile String createDate;
public String getLogId() {
return logId;
......
......@@ -18,9 +18,22 @@ import org.springframework.stereotype.Component;
*/
@Aspect
@Component
public class FeignAspect extends BaseRequestAspect {
public class AspectFeign {
private static final String TAG = FeignAspect.class.getSimpleName();
private static final String TAG = AspectFeign.class.getSimpleName();
private final AspectLogUrl aspectLogUrl;
private final AspectLogBody aspectLogBody;
private final AspectLogStart aspectLogStart;
private final AspectLogResult aspectLogResult;
public AspectFeign(AspectLogUrl aspectLogUrl, AspectLogBody aspectLogBody,
AspectLogStart aspectLogStart, AspectLogResult aspectLogResult) {
this.aspectLogUrl = aspectLogUrl;
this.aspectLogBody = aspectLogBody;
this.aspectLogStart = aspectLogStart;
this.aspectLogResult = aspectLogResult;
}
/**
* AOP的表达式
......@@ -32,24 +45,25 @@ public class FeignAspect extends BaseRequestAspect {
/**
* 执行环形切面
*
* @param joinPoint
* @return
* @param joinPoint 切面对象
* @return 切面结果
*/
@Around(value = "feignAspect()")
public Object requestFeignAround(ProceedingJoinPoint joinPoint) throws Throwable {
Class declaringType = joinPoint.getSignature().getDeclaringType();
String url = getMethodUrl(joinPoint);
String requestBody = getRequestBody(joinPoint);
boolean clear = requestLogInit();
long start = requestLog(declaringType, TAG, url, requestBody, clear);
LogVo log = startLog(TAG, url, requestBody);
Class<?> declaringType = joinPoint.getSignature().getDeclaringType();
String url = aspectLogUrl.getMethodUrl(joinPoint);
Object requestBody = aspectLogBody.getRequestBody(joinPoint);
boolean clear = aspectLogStart.requestLogInit();
LogVo log = new LogVo();
aspectLogStart.requestLog(declaringType, TAG, url, requestBody, log, clear);
long start = System.currentTimeMillis();
Object result = null;
Exception ex = null;
try {
result = joinPoint.proceed();
// 假如是标准格式,则验证接口是否成功,不成功则抛出异常
if (result instanceof ResponseResult) {
ResponseResult responseResult = (ResponseResult) result;
......@@ -67,7 +81,7 @@ public class FeignAspect extends BaseRequestAspect {
ex = e;
throw e;
} finally {
responseLog(declaringType, TAG, url, clear, start, getRequestBody(joinPoint), result, ex, log);
aspectLogResult.responseLog(declaringType, TAG, url, clear, start, requestBody, result, ex, log);
}
}
......
package com.yanzuoguang.cloud.aop;
import com.rabbitmq.client.Channel;
import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.vo.YzgFileUploadReqVo;
import com.yanzuoguang.util.helper.JsonHelper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
/**
* 配置文件处理
*
* @author 颜佐光
*/
@Component
public class AspectLogBody {
private final CloudConfig cloudConfig;
public AspectLogBody(CloudConfig cloudConfig) {
this.cloudConfig = cloudConfig;
}
/**
* 获取JSON,当Json过长时,截断
*
* @param paraJson 长json字符串
* @return 短json字符串
*/
public String getMaxString(String paraJson) {
if (paraJson != null && paraJson.length() > this.cloudConfig.getReqSize()) {
paraJson = paraJson.substring(0, this.cloudConfig.getReqSize());
}
return paraJson;
}
/**
* 获取内容转换为字符串
*
* @param body 内容
* @return 字符串
*/
public String getBodyString(Object body) {
String ret;
if (body instanceof String) {
ret = (String) body;
} else {
ret = JsonHelper.serialize(body);
}
ret = getMaxString(ret);
return ret;
}
/**
* 获取请求内容
*
* @param joinPoint 方法
* @return 方法的请求参数
*/
public Object getRequestBody(ProceedingJoinPoint joinPoint) {
return getFirstDataParameter(joinPoint.getArgs());
}
/**
* 获取请求的参数
*
* @param args
* @return
*/
private Object getFirstDataParameter(Object[] args) {
// 获取请求参数
List<Object> para = getDataParameters(args);
Object paraTo = para;
if (para.size() == 1) {
paraTo = para.get(0);
}
return paraTo;
}
/**
* 获取数据参数
*
* @param args 所有参数
* @return 有效参数
*/
private 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
|| item instanceof Message
|| item instanceof Channel
|| item instanceof YzgFileUploadReqVo
) {
continue;
}
para.add(item);
}
return para;
}
}
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.aop.log.LogLocal;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.thread.ThreadHelper;
import com.yanzuoguang.util.vo.LogVo;
import com.yanzuoguang.util.vo.ResponseResult;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
* 写入日志
*
* @author 颜佐光
*/
@Component
public class AspectLogResult {
private final CloudConfig cloudConfig;
private final LogLocal logLocal;
private final AspectLogBody aspectLogBody;
private final ResponseResult<Object> responseDefault = new ResponseResult();
public AspectLogResult(CloudConfig cloudConfig, LogLocal logLocal, AspectLogBody aspectLogBody) {
this.cloudConfig = cloudConfig;
this.logLocal = logLocal;
this.aspectLogBody = aspectLogBody;
}
/**
* 保存日志
*
* @param cls 类型
* @param tag 标签名称
* @param url 地址
* @param logFlag 请求日志
* @param start 开始时间
* @param requestBody 请求内容
* @param result 结果
* @param resultEx 异常结果
* @param log 内容
*/
@Async
public void responseLog(Class<?> cls, String tag, String url, boolean logFlag, long start,
Object requestBody, Object result, Exception resultEx, LogVo log) {
try {
if (StringHelper.isEmpty(log.getLogId())) {
// 等待线程同步,超过该时间则不继续等待
ThreadHelper.sleep(30);
}
String body = aspectLogBody.getBodyString(requestBody);
if (log != null && !StringHelper.compare(log.getContent(), body)) {
log.setContentTo(body);
}
long time = System.currentTimeMillis() - start;
boolean isLogDisplay = (logFlag && this.cloudConfig.isLogCommon()) || resultEx != null;
boolean isLogDatabase = log != null || resultEx != null;
// 处理结果
ResponseResult responseResult = responseDefault;
if (result instanceof ResponseResult) {
responseResult = (ResponseResult) result;
} else if (result != null) {
responseResult = ResponseResult.result(result);
}
if (isLogDisplay || isLogDatabase) {
// 执行时间
if (resultEx != null) {
responseResult = ExceptionHelper.getError(resultEx);
}
String json = JsonHelper.serialize(responseResult);
if (isLogDisplay) {
Log.error(cls, resultEx, "%s [ %s ] time %d ms, result: %s", tag, url, time, aspectLogBody.getMaxString(json));
}
if (isLogDatabase) {
logLocal.result(log, responseResult.getCode(), json);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (logFlag) {
Log.threadCommit();
}
}
}
}
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.aop.log.LogLocal;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.LogVo;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
* 写入日志
*
* @author 颜佐光
*/
@Component
public class AspectLogStart {
private final CloudConfig cloudConfig;
private final LogLocal logLocal;
private final AspectLogBody aspectLogBody;
public AspectLogStart(CloudConfig cloudConfig, LogLocal logLocal, AspectLogBody aspectLogBody) {
this.cloudConfig = cloudConfig;
this.logLocal = logLocal;
this.aspectLogBody = aspectLogBody;
}
/**
* 记录请求日志
*
* @param tag
* @param url
* @param requestBody
* @param log
* @param logFlag
* @return
*/
@Async
public void requestLog(Class<?> cls, String tag, String url, Object requestBody, LogVo log, boolean logFlag) {
try {
String body = aspectLogBody.getBodyString(requestBody);
body = aspectLogBody.getMaxString(body);
if (logFlag) {
this.logLocal.startLog(log, tag, String.format("%s:%s", this.cloudConfig.getApplicationName(), url), body);
}
boolean isLog = logFlag && this.cloudConfig.isLogCommon();
if (isLog) {
Log.info(cls, " %s [ %s ] request: %s", tag, url, body);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 获取是否清空日志的标签
*
* @return 返回日志是否清空
*/
protected boolean requestLogInit() {
boolean clear = Log.threadCurrent() == null;
if (clear) {
Log.threadBegin();
}
return clear;
}
}
package com.yanzuoguang.cloud.aop;
import com.rabbitmq.client.Channel;
import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.aop.log.LogLocal;
import com.yanzuoguang.cloud.vo.YzgFileUploadReqVo;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.ArrayHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
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;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* 基本处理拦截类
* 配置文件处理
*
* @author 颜佐光
*/
public class BaseRequestAspect {
@Component
public class AspectLogUrl {
@Autowired
protected LogLocal logLocal;
private final CloudConfig cloudConfig;
@Autowired
protected CloudConfig cloudConfig;
private final ResponseResult responseDefault = new ResponseResult();
/**
* 开始记录日志对象
*
* @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.cloudConfig.getApplicationName(), url), body);
}
/**
* 获取请求内容
*
* @param joinPoint
* @return
*/
protected String getRequestBody(ProceedingJoinPoint joinPoint) {
Object firstParameter = getFirstDataParameter(joinPoint.getArgs());
if (firstParameter instanceof String) {
return (String) firstParameter;
}
return JsonHelper.serialize(firstParameter);
}
/**
* 获取请求的参数
*
* @param args
* @return
*/
protected Object getFirstDataParameter(Object[] args) {
// 获取请求参数
List<Object> para = getDataParameters(args);
Object paraTo = para;
if (para.size() == 1) {
paraTo = para.get(0);
}
return paraTo;
}
/**
* 获取数据参数
*
* @param args
* @return
*/
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
|| item instanceof Message
|| item instanceof Channel
|| item instanceof YzgFileUploadReqVo
) {
continue;
}
para.add(item);
}
return para;
}
/**
* 根据类型获取对象
*
* @param args
* @return
*/
protected Message getMessage(Object[] args) {
for (Object item : args) {
if (item instanceof Message) {
return (Message) item;
}
}
return null;
}
/**
* 获取JSON,当Json过长时,截断
*
* @param paraJson
* @return
*/
private String getMaxString(String paraJson) {
if (paraJson != null && paraJson.length() > this.cloudConfig.getReqSize()) {
paraJson = paraJson.substring(0, this.cloudConfig.getReqSize());
}
return paraJson;
}
/**
* 获取是否清空日志的标签
*
* @return 返回日志是否清空
*/
protected boolean requestLogInit() {
boolean clear = Log.threadCurrent() == null;
if (clear) {
Log.threadBegin();
}
return clear;
}
/**
* 记录请求日志
*
* @param tag
* @param url
* @param requestBody
* @param logFlag
* @return
*/
protected long requestLog(Class<?> cls, String tag, String url, String requestBody, boolean logFlag) {
long start = System.currentTimeMillis();
try {
boolean isLog = logFlag && this.cloudConfig.isLogCommon();
if (isLog) {
Log.info(cls, " %s [ %s ] request: %s", tag, url, this.getMaxString(requestBody));
}
} catch (Exception ex) {
ex.printStackTrace();
}
return start;
}
protected void writeLogBody(LogVo log, String requestBody) {
}
/**
* 保存日志
*
* @param cls
* @param tag
* @param url
* @param logFlag
* @param start
* @param requestBody
* @param result
* @param resultEx
* @param log
*/
protected void responseLog(Class<?> cls, String tag, String url, boolean logFlag, long start,
String requestBody, Object result, Exception resultEx, LogVo log) {
try {
if (log != null && !StringHelper.compare(log.getContent(), requestBody)) {
log.setContentTo(requestBody);
}
long time = System.currentTimeMillis() - start;
boolean isLogDisplay = (logFlag && this.cloudConfig.isLogCommon()) || resultEx != null;
boolean isLogDatabase = log != null || resultEx != null;
// 处理结果
ResponseResult responseResult = responseDefault;
if (result instanceof ResponseResult) {
responseResult = (ResponseResult) result;
} else if (result != null) {
responseResult = ResponseResult.result(result);
}
if (isLogDisplay || isLogDatabase) {
// 执行时间
if (resultEx != null) {
responseResult = ExceptionHelper.getError(resultEx);
}
String json = JsonHelper.serialize(responseResult);
if (isLogDisplay) {
Log.error(cls, resultEx, "%s [ %s ] time %d ms, result: %s", tag, url, time, getMaxString(json));
}
if (isLogDatabase) {
logLocal.result(log, responseResult.getCode(), json);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (logFlag) {
Log.threadCommit();
}
}
public AspectLogUrl(CloudConfig cloudConfig) {
this.cloudConfig = cloudConfig;
}
/**
* /**
* 获取方法名称
*
* @return
*/
protected String getMethodUrl(ProceedingJoinPoint joinPoint) {
public String getMethodUrl(ProceedingJoinPoint joinPoint) {
String url = StringHelper.EMPTY;
Signature signature = joinPoint.getSignature();
Class declaringType = signature.getDeclaringType();
......@@ -311,4 +115,20 @@ public class BaseRequestAspect {
}
return sb.toString();
}
/**
* 根据类型获取对象
*
* @param args
* @return
*/
public Message getMessage(Object[] args) {
for (Object item : args) {
if (item instanceof Message) {
return (Message) item;
}
}
return null;
}
}
......@@ -11,7 +11,6 @@ import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
......@@ -24,12 +23,25 @@ import java.lang.reflect.Method;
*/
@Aspect
@Component
public class MqAspect extends BaseRequestAspect {
public class AspectMq {
private static final String TAG = MqAspect.class.getSimpleName();
private static final String TAG = AspectMq.class.getSimpleName();
@Autowired
private MessageLogService logService;
private final MessageLogService logService;
private final AspectLogUrl aspectLogUrl;
private final AspectLogBody aspectLogBody;
private final AspectLogStart aspectLogStart;
private final AspectLogResult aspectLogResult;
public AspectMq(MessageLogService logService,
AspectLogUrl aspectLogUrl, AspectLogBody aspectLogBody,
AspectLogStart aspectLogStart, AspectLogResult aspectLogResult) {
this.logService = logService;
this.aspectLogUrl = aspectLogUrl;
this.aspectLogBody = aspectLogBody;
this.aspectLogStart = aspectLogStart;
this.aspectLogResult = aspectLogResult;
}
/**
* exec aop point aspect
......@@ -47,15 +59,16 @@ public class MqAspect extends BaseRequestAspect {
*/
@Around(value = "mqAspect()")
public Object requestMqAround(ProceedingJoinPoint joinPoint) throws Throwable {
Class declaringType = joinPoint.getSignature().getDeclaringType();
String url = aspectLogUrl.getMethodUrl(joinPoint);
Object requestBody = aspectLogBody.getRequestBody(joinPoint);
boolean clear = isRabbit(joinPoint);
if (clear) {
clear = requestLogInit();
clear = aspectLogStart.requestLogInit();
}
Class declaringType = joinPoint.getSignature().getDeclaringType();
String url = getMethodUrl(joinPoint);
String requestBody = getRequestBody(joinPoint);
long start = requestLog(declaringType, TAG, url, requestBody, clear);
LogVo log = startLog(TAG, url, requestBody);
LogVo log = new LogVo();
aspectLogStart.requestLog(declaringType, TAG, url, requestBody, log, clear);
long start = System.currentTimeMillis();
Object result = null;
Exception ex = null;
......@@ -69,10 +82,16 @@ public class MqAspect extends BaseRequestAspect {
throw e;
} finally {
logService.logCurrentRemove();
responseLog(declaringType, TAG, url, clear, start, getRequestBody(joinPoint), result, ex, log);
aspectLogResult.responseLog(declaringType, TAG, url, clear, start, joinPoint, result, ex, log);
}
}
/**
* 判断是否MQ函数
*
* @param joinPoint 函数
* @return 是否MQ函数
*/
private boolean isRabbit(ProceedingJoinPoint joinPoint) {
boolean ret = false;
if (!(joinPoint.getSignature() instanceof MethodSignature)) {
......@@ -80,7 +99,7 @@ public class MqAspect extends BaseRequestAspect {
}
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method targetMethod = methodSignature.getMethod();
Message message = getMessage(joinPoint.getArgs());
Message message = aspectLogUrl.getMessage(joinPoint.getArgs());
if (message != null) {
logService.logCurrent(message);
}
......@@ -95,6 +114,13 @@ public class MqAspect extends BaseRequestAspect {
return ret;
}
/**
* 执行函数
*
* @param joinPoint
* @return
* @throws Throwable
*/
private Object executeMethod(ProceedingJoinPoint joinPoint) throws Throwable {
return joinPoint.proceed();
}
......
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.service.TokenServiceCall;
import com.yanzuoguang.token.TokenHelper;
import com.yanzuoguang.util.exception.ExceptionHelper;
......@@ -14,15 +15,11 @@ 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.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.*;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Optional;
/**
* LogsAspect(接口请求日志切面)
......@@ -32,42 +29,33 @@ import java.lang.reflect.Type;
*/
@Aspect
@Component
public class WebAspect extends BaseRequestAspect implements ApplicationContextAware {
public class AspectWeb {
private static final String TAG = WebAspect.class.getSimpleName();
private static final String TAG = AspectWeb.class.getSimpleName();
@Autowired
private TokenServiceCall tokenServiceCall;
/**
* 是否存在初始化方法
*/
private WebAspectInit webAspectInit;
private final TokenServiceCall tokenServiceCall;
private final WebAspectInit webAspectInit;
private final CloudConfig cloudConfig;
private final AspectLogUrl aspectLogUrl;
private final AspectLogBody aspectLogBody;
private final AspectLogStart aspectLogStart;
private final AspectLogResult aspectLogResult;
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
try {
this.webAspectInit = applicationContext.getBean(WebAspectInit.class);
} catch (Exception ex) {
webAspectInit = new WebAspectInit() {
@Override
public void init(Object req) {
}
public AspectWeb(TokenServiceCall tokenServiceCall, Optional<WebAspectInit> webAspectInit, CloudConfig cloudConfig,
AspectLogUrl aspectLogUrl, AspectLogBody aspectLogBody,
AspectLogStart aspectLogStart, AspectLogResult aspectLogResult) {
this.tokenServiceCall = tokenServiceCall;
this.cloudConfig = cloudConfig;
this.aspectLogUrl = aspectLogUrl;
this.aspectLogBody = aspectLogBody;
this.aspectLogStart = aspectLogStart;
this.aspectLogResult = aspectLogResult;
if (webAspectInit.isPresent()) {
this.webAspectInit = webAspectInit.get();
} else {
this.webAspectInit = req -> {
};
Log.error(WebAspect.class, "请设置登录默认处理函数,实现 WebAspectInit 接口");
Log.error(AspectWeb.class, "请设置登录默认处理函数,实现 WebAspectInit 接口");
}
}
......@@ -81,28 +69,29 @@ public class WebAspect extends BaseRequestAspect implements ApplicationContextAw
/**
* 执行环形切面
*
* @param joinPoint
* @return
* @param joinPoint 切面对象
* @return 切面结果
*/
@Around(value = "webAspect()")
public Object requestWebAround(ProceedingJoinPoint joinPoint) throws Throwable {
Object result = null;
// 判断是否网关
boolean isGateWay = cloudConfig.isGateWay();
if (isGateWay) {
result = executeMethod(joinPoint);
return result;
// 网关不进行任何拦截处理
return executeMethod(joinPoint);
}
Class declaringType = joinPoint.getSignature().getDeclaringType();
String url = getMethodUrl(joinPoint);
String requestBody = getRequestBody(joinPoint);
LogVo log = null;
boolean clear = requestLogInit();
long start = requestLog(declaringType, TAG, url, requestBody, clear);
if (clear) {
log = startLog(TAG, url, requestBody);
}
String url = aspectLogUrl.getMethodUrl(joinPoint);
Object requestBody = aspectLogBody.getRequestBody(joinPoint);
boolean clear = aspectLogStart.requestLogInit();
LogVo log = new LogVo();
aspectLogStart.requestLog(declaringType, TAG, url, requestBody, log, clear);
long start = System.currentTimeMillis();
Exception ex = null;
boolean isInit = false;
Object result = null;
try {
// 请求登录服务初始化
isInit = tokenServiceCall.tokenInit();
......@@ -131,12 +120,7 @@ public class WebAspect extends BaseRequestAspect implements ApplicationContextAw
if (isInit) {
TokenHelper.remove();
}
// 获取最终的处理结果,写入到日志
try {
requestBody = getRequestBody(joinPoint);
} catch (Exception e) {
}
responseLog(declaringType, TAG, url, clear, start, requestBody, result, ex, log);
aspectLogResult.responseLog(declaringType, TAG, url, clear, start, requestBody, result, ex, log);
}
}
......
......@@ -7,7 +7,6 @@ import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.thread.ThreadNext;
import com.yanzuoguang.util.vo.LogVo;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
......@@ -23,6 +22,11 @@ import java.util.concurrent.ConcurrentHashMap;
*/
@Component
public class LogLocal implements ThreadNext.Next, InitializingBean {
/**
* 日志基础
*/
private final LogBase logBase;
private final CloudConfig cloudConfig;
/**
* 超时状态
*/
......@@ -32,14 +36,11 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
* 缓存队列
*/
protected volatile Map<String, Timeout<LogVo>> cache = new ConcurrentHashMap<>();
/**
* 日志基础
*/
@Autowired
private LogBase logBase;
@Autowired
private CloudConfig cloudConfig;
public LogLocal(LogBase logBase, CloudConfig cloudConfig) {
this.logBase = logBase;
this.cloudConfig = cloudConfig;
}
/**
* Invoked by a BeanFactory after it has set all bean properties supplied
......@@ -59,34 +60,30 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
/**
* 开始记录日志
*
* @param log 日志对象
* @param actionKey 功能名称
* @param actionSubKey 子功能名称 = 服务名称 + 地址
* @param content 请求内容
* @return
*/
public LogVo startLog(String actionKey, String actionSubKey, String content) {
if (isLog(actionKey, actionSubKey)) {
return null;
}
// 生命日志对象爱嗯
LogVo log = new LogVo();
log.setLogId(StringHelper.getNewID());
public void startLog(LogVo log, String actionKey, String actionSubKey, String content) {
// 写入其他对象
log.setActionKey(actionKey);
log.setActionSubKey(actionSubKey);
log.setContent(content);
// 声明超时对象
Timeout<LogVo> time = new Timeout<>(log);
log.setCreateDate(DateHelper.getDateTimeString(new Date(time.getStart())));
// 生命日志对象爱嗯
log.setLogId(StringHelper.getNewID());
cache.put(log.getLogId(), time);
return log;
}
/**
* 写入状态
*
* @param status
* @param result
* @return
* @param log 日志对象
* @param status 状态
* @param result 结果
*/
public void result(LogVo log, String status, String result) {
if (log == null) {
......@@ -105,11 +102,13 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
/**
* 写入状态
*
* @param status
* @param result
* @return
* @param timeout 超时
* @param log 日志对象
* @param status 状态
* @param result 结果
* @param write 写入
*/
private void result(Timeout timeout, LogVo log, String status, String result, boolean write) {
private void result(Timeout<LogVo> timeout, LogVo log, String status, String result, boolean write) {
if (timeout != null) {
long useTime = System.currentTimeMillis() - timeout.getStart();
log.setUseTime((int) useTime);
......@@ -127,7 +126,7 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
/**
* 删除请求信息
*
* @param log
* @param log 删除日志
*/
private void remove(LogVo log) {
Timeout<LogVo> timeout = cache.get(log.getLogId());
......@@ -137,7 +136,7 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
/**
* 记录超时
*
* @return
* @return 超时对象
*/
private void writeTimeout(Timeout<LogVo> timeout) {
result(timeout, timeout.getData(), MAX_TIME, MAX_TIME_NAME, true);
......
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