当前位置:网站首页>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);
}
边栏推荐
- Installation and use of blue lake
- Spring moves are coming. Watch the gods fight
- Lei Jun wrote a blog when he was a programmer. It's awesome
- Feature Engineering: summary of common feature transformation methods
- [JS -- map string]
- pip 安装第三方库
- Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
- Www 2022 | rethinking the knowledge map completion of graph convolution network
- [C language] Dynamic Planning --- from entry to standing up
- 万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
猜你喜欢
![Learn AI safety monitoring project from zero [attach detailed code]](/img/a9/cb93f349229e86cbb05ad196ae9553.jpg)
Learn AI safety monitoring project from zero [attach detailed code]

Websites that it people often visit
![[JS event -- event flow]](/img/fe/199890b082845f68b65f25056e6f29.jpg)
[JS event -- event flow]

BGP experiment the next day

万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!

First acquaintance with P4 language

Typescript practice for SAP ui5

Keil compilation code of CY7C68013A

Www2022 | know your way back: self training method of graph neural network under distribution and migration

记录一次Unity 2020.3.31f1的bug
随机推荐
Force buckle 540 A single element in an ordered array
Binary tree problem solving (2)
Play with concurrency: what's the use of interruptedexception?
Typescript practice for SAP ui5
Keil compilation code of CY7C68013A
Research on the security of ognl and El expressions and memory horse
千亿市场规模医疗美容行业的水究竟有多浑?
蓝湖的安装及使用
60后关机程序
二叉树解题(二)
geotrust ov多域名ssl证书一年两千一百元包含几个域名?
The difference between vectorresize and reverse.
阿里云polkit pkexec 本地提权漏洞
Alibaba cloud polkit pkexec local rights lifting vulnerability
66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
Use of go package
Document declaration and character encoding
C语言猜数字游戏
Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)