当前位置:网站首页>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);
}
边栏推荐
- 二叉树解题(二)
- 万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
- 66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
- Message mechanism -- message processing
- There is no prompt for SQL in idea XML, and the dialect setting is useless.
- Wechat applet map annotation
- C language practice - number guessing game
- Play with concurrency: what's the use of interruptedexception?
- GeoTrust ov multi domain SSL certificate is 2100 yuan a year. How many domain names does it contain?
- [C language] Dynamic Planning --- from entry to standing up
猜你喜欢

Exposure X8标准版图片后期滤镜PS、LR等软件的插件

Installation and use of blue lake

文档声明与字符编码

Pytorch---使用Pytorch实现U-Net进行语义分割

Keil compilation code of CY7C68013A

Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial

Www 2022 | rethinking the knowledge map completion of graph convolution network

6月书讯 | 9本新书上市,阵容强大,闭眼入!

Common sense of cloud server security settings

Websites that it people often visit
随机推荐
Realizing deep learning framework from zero -- Introduction to neural network
Ognl和EL表达式以及内存马的安全研究
Is it safe to open an account with first venture securities? I like to open an account. How can I open it?
[graduation season · advanced technology Er] young people have dreams, why are they afraid of hesitation
【c语言】动态规划---入门到起立
First acquaintance with P4 language
Shutdown procedure after 60
Ten thousand volumes are known to all, and one page of a book is always relevant. TVP reading club will take you through the reading puzzle!
The solution to the complexity brought by lambda expression
Installation and use of blue lake
LeetCode-对链表进行插入排序
66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
Li Kou interview question 02.08 Loop detection
Uni app - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
【提高课】ST表解决区间最值问题【2】
Binary tree problem solving (2)
okcc为什么云呼叫中心比传统呼叫中心更好?
Alibaba cloud polkit pkexec local rights lifting vulnerability
Pytorch yolov5 exécute la résolution de bogues à partir de 0:
Common locks in MySQL