当前位置:网站首页>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);
}
边栏推荐
- The solution to the complexity brought by lambda expression
- June book news | 9 new books are listed, with a strong lineup and eyes closed!
- Major domestic quantitative trading platforms
- Play with concurrency: draw a thread state transition diagram
- Playing with concurrency: what are the ways of communication between threads?
- Exposure X8标准版图片后期滤镜PS、LR等软件的插件
- C language guessing numbers game
- Yyds dry goods inventory kubernetes introduction foundation pod concept and related operations
- office_ Delete the last page of word (the seemingly blank page)
- [improvement class] st table to solve the interval maximum value problem [2]
猜你喜欢
Unit testing classic three questions: what, why, and how?
Why can't you remember when reading? Why can't you remember- My technology learning methodology
Typescript practice for SAP ui5
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
6月书讯 | 9本新书上市,阵容强大,闭眼入!
One step implementation of yolox helmet detection (combined with oak intelligent depth camera)
万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
Fluent icon demo
unable to execute xxx. SH: operation not permitted
Dare to go out for an interview without learning some distributed technology?
随机推荐
初识P4语言
[C language] Dynamic Planning --- from entry to standing up
Wechat applet map annotation
Binary tree problem solving (1)
okcc为什么云呼叫中心比传统呼叫中心更好?
Landing guide for "prohibit using select * as query field list"
LCM of Spreadtrum platform rotates 180 °
cs架构下抓包的几种方法
Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
Dare to go out for an interview without learning some distributed technology?
Introduction to vmware workstation and vSphere
Pytorch yolov5 exécute la résolution de bogues à partir de 0:
[JS -- map string]
The core idea of performance optimization, dry goods sharing
C language practice - binary search (half search)
Go function
Why can't you remember when reading? Why can't you remember- My technology learning methodology
第十六周作业
Today's plan: February 15, 2022
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial