Skip to content

Commit

Permalink
feat: log requesting user id when logging err
Browse files Browse the repository at this point in the history
See: BEDS-94
  • Loading branch information
LuccaBitfly committed Oct 23, 2024
1 parent 0574a2e commit 149f640
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions backend/pkg/api/handlers/handler_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,17 @@ func returnGone(w http.ResponseWriter, r *http.Request, err error) {
const maxBodySize = 10 * 1024

func logApiError(r *http.Request, err error, callerSkip int, additionalInfos ...log.Fields) {
body, _ := io.ReadAll(io.LimitReader(r.Body, maxBodySize))
requestFields := log.Fields{
"request_endpoint": r.Method + " " + r.URL.Path,
"request_query": r.URL.RawQuery,
"request_body": string(body),
}
if len(r.URL.RawQuery) > 0 {
requestFields["request_query"] = r.URL.RawQuery
}
if body, _ := io.ReadAll(io.LimitReader(r.Body, maxBodySize)); len(body) > 0 {
requestFields["request_body"] = string(body)
}
if userId, _ := GetUserIdByContext(r); userId != 0 {
requestFields["request_user_id"] = userId
}
log.Error(err, "error handling request", callerSkip+1, append(additionalInfos, requestFields)...)
}
Expand Down

0 comments on commit 149f640

Please sign in to comment.