Spring Boot - 如何在一个地方记录所有请求和异常响应?
2022-08-31 04:58:17
我正在开发带有弹簧启动的REST API。我需要使用输入参数记录所有请求(例如,使用方法。GET、POST 等),请求路径、查询字符串、此请求的对应类方法,以及此操作的响应,既有成功也有错误。例如:
请求成功:
http://example.com/api/users/1
日志应如下所示:
{
HttpStatus: 200,
path: "api/users/1",
method: "GET",
clientIp: "0.0.0.0",
accessToken: "XHGu6as5dajshdgau6i6asdjhgjhg",
method: "UsersController.getUser",
arguments: {
id: 1
},
response: {
user: {
id: 1,
username: "user123",
email: "user123@example.com"
}
},
exceptions: []
}
或请求有错误:
http://example.com/api/users/9999
日志应如下所示:
{
HttpStatus: 404,
errorCode: 101,
path: "api/users/9999",
method: "GET",
clientIp: "0.0.0.0",
accessToken: "XHGu6as5dajshdgau6i6asdjhgjhg",
method: "UsersController.getUser",
arguments: {
id: 9999
},
returns: {
},
exceptions: [
{
exception: "UserNotFoundException",
message: "User with id 9999 not found",
exceptionId: "adhaskldjaso98d7324kjh989",
stacktrace: ...................
]
}
我希望请求/响应是单个实体,在成功和错误情况下都具有与此实体相关的自定义信息。
在春季实现这一目标的最佳实践是什么,可能是用过滤器?如果是,你能提供具体的例子吗?
我已经玩过 和 ,但正如我所提到的,我需要在一个地方(和单个日志)处理所有成功和错误请求。@ControllerAdvice
@ExceptionHandler