私はスプリングブートで残りの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: ...................
]
}
Request/Response を、成功した場合とエラーの場合の両方で、このエンティティに関連するカスタム情報を持つ単一のエンティティにしたいと考えています。
これを達成するための春のベストプラクティスは何ですか?フィルターを使用する可能性がありますか? はいの場合、具体例を挙げていただけますか?
@ControllerAdvice
とで遊んだことが@ExceptionHandler
ありますが、前述したように、すべての成功とエラーの要求を 1 か所 (および 1 つのログ) で処理する必要があります。