feat:[REQ-3282] 增加feign日志打印
This commit is contained in:
parent
3a7e8593be
commit
76cbcb0ef3
@ -0,0 +1,101 @@
|
|||||||
|
package cn.axzo.orgmanax.integration.config;
|
||||||
|
|
||||||
|
import cn.azxo.framework.common.utils.LogUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.Signature;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.mapstruct.ap.internal.util.Collections;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author liting@axzo.cn
|
||||||
|
* @version 1.0.0
|
||||||
|
* @Descpriont feign日志打印
|
||||||
|
* @see FeignClientLogAspect
|
||||||
|
* @since 1.0.0 2022-09-16
|
||||||
|
*/
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class FeignClientLogAspect {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 切入含有@FeignClient注解的类
|
||||||
|
*/
|
||||||
|
@Around(value = "@within(feignClient)")
|
||||||
|
public Object classHandler(ProceedingJoinPoint pjp, FeignClient feignClient) throws Throwable {
|
||||||
|
Long startTime = System.currentTimeMillis();
|
||||||
|
this.logRequest(pjp, feignClient);
|
||||||
|
Object response = null;
|
||||||
|
try {
|
||||||
|
response = pjp.proceed();
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
String bizInfo = getBizInfo(feignClient, true);
|
||||||
|
String signature = getSignature(pjp);
|
||||||
|
Object[] params = pjp.getArgs();
|
||||||
|
log.warn("[{}]{}, param={}, 调用时发生系统异常:{}", bizInfo, signature, JSON.toJSONString(params), throwable.getStackTrace());
|
||||||
|
LogUtil.error(LogUtil.ErrorType.ERROR_THIRD_SERVICE, String.format("[%s]%s调用时发生系统异常", bizInfo, signature), throwable.getMessage());
|
||||||
|
throw throwable;
|
||||||
|
}
|
||||||
|
this.logResponse(startTime, response, pjp, feignClient);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void logRequest(ProceedingJoinPoint pjp, FeignClient feignClient) {
|
||||||
|
String bizInfo = this.getBizInfo(feignClient, true);
|
||||||
|
Object[] params = pjp.getArgs();
|
||||||
|
String signature = this.getSignature(pjp);
|
||||||
|
|
||||||
|
log.info("[{}] {} 请求开始param={}", new Object[]{bizInfo, signature, JSON.toJSONString(params)});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void logResponse(long startTime, Object response, ProceedingJoinPoint pjp, FeignClient feignClient) {
|
||||||
|
long endTime = System.currentTimeMillis();
|
||||||
|
String signatureInfo = this.getSignature(pjp);
|
||||||
|
String costTime = endTime - startTime + "ms";
|
||||||
|
String bizInfo = this.getBizInfo(feignClient, false);
|
||||||
|
if (response != null) {
|
||||||
|
log.info("[{}] {} 请求结束response={},COST : {}", new Object[]{bizInfo, signatureInfo, JSON.toJSONString(response), costTime});
|
||||||
|
} else {
|
||||||
|
log.info("[{}] {} 请求结束response为null,COST : {}", new Object[]{bizInfo, signatureInfo, costTime});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getSignature(ProceedingJoinPoint pjp) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
Signature signature = pjp.getSignature();
|
||||||
|
String typeName = signature.getDeclaringType().getSimpleName();
|
||||||
|
String methodName = signature.getName();
|
||||||
|
sb.append(typeName).append("#").append(methodName);
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getBizInfo(FeignClient feignClient, boolean request) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
String source = SpringUtil.getApplicationName();
|
||||||
|
String target = feignClient.name();
|
||||||
|
if (StrUtil.isNotBlank(source) && StrUtil.isNotBlank(target)) {
|
||||||
|
sb.append(source);
|
||||||
|
if (request) {
|
||||||
|
sb.append(" -> ");
|
||||||
|
} else {
|
||||||
|
sb.append(" <- ");
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.append(target).append(":");
|
||||||
|
return sb.toString();
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,37 @@
|
|||||||
|
package cn.axzo.orgmanax.server.node.controller;
|
||||||
|
|
||||||
|
import cn.axzo.foundation.result.ApiResult;
|
||||||
|
import cn.axzo.orgmanax.infra.client.elise.EliseDeviceClient;
|
||||||
|
import cn.axzo.orgmanax.infra.client.gaia.GaiaOrderServiceClient;
|
||||||
|
import cn.axzo.orgmanax.infra.client.gaia.dto.GaiaListWorkerUnFinishWorkpointResp;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@RequestMapping("test")
|
||||||
|
@RestController
|
||||||
|
public class TestController {
|
||||||
|
|
||||||
|
private final GaiaOrderServiceClient gaiaOrderServiceClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试接口
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping
|
||||||
|
public ApiResult<Void> process() {
|
||||||
|
List<GaiaListWorkerUnFinishWorkpointResp> gaiaListWorkerUnFinishWorkpointResps = gaiaOrderServiceClient.listWorkerUnFinishWorkpoint(195L, 10680L);
|
||||||
|
return ApiResult.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user