当前位置:网站首页>Design and implementation of general interface open platform - (44) log processing of API services
Design and implementation of general interface open platform - (44) log processing of API services
2022-07-02 04:30:00 【lwq2025】
I won't say much about the importance of logs , For time tracing and troubleshooting , We save the complete data of the request and response of the interface service call in the database , And for the convenience of performance analysis , The time difference between receiving the request and returning the response of each interface call is also saved , This point , It is very important in practical work , Early warning and prediction can be made , Find problems before the interface slows or crashes .
Whether the result of the interface call is success or failure , Normal or abnormal , We all hope to record the log completely , therefore , Processing of logs , Not through filters , Instead, it is put into the exception handling finally In block .
/** * ' * API Service technology framework implementation * * @author wqliu * @date 2022-2-10 14:44 **/
@Service
public class ApiRestServiceImpl implements ApiRestService {
private ApiFilterChain filterChain;
@Autowired
private ApiServiceLogService apiServiceLogService;
public ApiRestServiceImpl(FrameworkValidateFilter frameworkValidateFilter,
BusinessFilter businessFilter, BasicValidateFilter basicValidateFilter) {
filterChain=new ApiFilterChain(basicValidateFilter,frameworkValidateFilter,businessFilter);
}
@Override
public ApiResponse handle(ApiRequest apiRequest) {
LocalDateTime receiveTime = LocalDateTime.now();
ApiResponse apiResponse = new ApiResponse();
try {
filterChain.doFilter(apiRequest,apiResponse);
apiResponse = this.filterChain.getResponse();
apiResponse.setExecuteResult(ApiServiceExecuteResultEnum.SUCCESS.name());
} catch (CustomException ex) {
// Custom exception handling
apiResponse.setExecuteResult(ApiServiceExecuteResultEnum.ERROR.name());
apiResponse.setErrorCode("S00");
apiResponse.setErrorMessage(ex.getMessage());
} catch (ApiException ex) {
//API exception handling
apiResponse.setExecuteResult(ApiServiceExecuteResultEnum.ERROR.name());
apiResponse.setErrorCode(ex.getErrorCode());
apiResponse.setErrorMessage(ex.getMessage());
} catch (Exception ex) {
// Unexpected exception handling
apiResponse.setExecuteResult(ApiServiceExecuteResultEnum.ERROR.name());
apiResponse.setErrorCode("S99");
apiResponse.setErrorMessage(" Undefined exception :" + ex.getMessage());
} finally {
// Reset required , For the next request
filterChain.reset();
// Log
apiServiceLogService.recordLog(apiRequest,apiResponse,receiveTime);
}
return apiResponse;
}
}
The log is saved as follows :
@Override
public void recordLog(ApiRequest request, ApiResponse response, LocalDateTime receiveTime) {
LocalDateTime responseTime = LocalDateTime.now();
ApiServiceLog apiServiceLog = new ApiServiceLog();
apiServiceLog.setAppCode(request.getAppCode());
apiServiceLog.setServiceCode(request.getServiceCode());
apiServiceLog.setRequestTime(DateUtils.toLocalDateTime(request.getRequestTime()));
apiServiceLog.setReceiveTime(receiveTime);
apiServiceLog.setRequestBusinessData(request.getData());
apiServiceLog.setExecuteResult(response.getExecuteResult());
apiServiceLog.setErrorCode(response.getErrorCode());
apiServiceLog.setErrorMessage(response.getErrorMessage());
apiServiceLog.setResponseTime(responseTime);
apiServiceLog.setResponseBusinessData(response.getData());
// Calculate execution time
apiServiceLog.setExecutionTime(Duration.between(receiveTime, responseTime).toMillis());
// preservation
add(apiServiceLog);
}
边栏推荐
- cookie、session、tooken
- 6月书讯 | 9本新书上市,阵容强大,闭眼入!
- 如何解决在editor模式下 无法删除物体的问题
- 深圳打造全球“鸿蒙欧拉之城”将加快培育生态,优秀项目最高资助 1000 万元
- Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
- Uni app - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
- 汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
- Li Kou interview question 02.08 Loop detection
- C语言猜数字游戏
- Play with concurrency: what's the use of interruptedexception?
猜你喜欢
PIP installation of third-party libraries
Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
10 minutes to understand CMS garbage collector in JVM
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
What methods should service define?
Sword finger offer II 006 Sort the sum of two numbers in the array
The solution to the complexity brought by lambda expression
BGP experiment the next day
Learn what definitelytyped is through the typescript development environment of SAP ui5
Yyds dry inventory compiler and compiler tools
随机推荐
初识P4语言
Cache consistency solution - how to ensure the consistency between the cache and the data in the database when changing data
【毕业季·进击的技术er】年少有梦,何惧彷徨
LCM of Spreadtrum platform rotates 180 °
Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
CY7C68013A之keil编译代码
Arbre binaire pour résoudre le problème (2)
Yyds dry inventory compiler and compiler tools
uni-app - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
Introduction to JSON usage scenarios and precautions
Exposure X8 Standard Version picture post filter PS, LR and other software plug-ins
MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
GeoTrust ov multi domain SSL certificate is 2100 yuan a year. How many domain names does it contain?
C language practice - binary search (half search)
Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
Pytorch---使用Pytorch进行鸟类的预测
二叉树解题(一)
What methods should service define?
Record the bug of unity 2020.3.31f1 once
Pytoch --- use pytoch to realize u-net semantic segmentation