Add request logging capabilities to API handlers and update .gitignore
Some checks failed
docker-image / docker (push) Has been cancelled
goreleaser / goreleaser (push) Has been cancelled

Enhance API response handling by storing responses in context and updating request logger to include API responses
This commit is contained in:
Luis Pater
2025-08-16 05:03:10 +08:00
parent 4155805ad6
commit fcadf08921
10 changed files with 191 additions and 26 deletions

View File

@@ -134,7 +134,7 @@ func (w *ResponseWriterWrapper) processStreamingChunks() {
}
// Finalize completes the logging process for the response.
func (w *ResponseWriterWrapper) Finalize() error {
func (w *ResponseWriterWrapper) Finalize(c *gin.Context) error {
if !w.logger.IsEnabled() {
return nil
}
@@ -171,6 +171,26 @@ func (w *ResponseWriterWrapper) Finalize() error {
finalHeaders[key] = values
}
var apiRequestBody []byte
apiRequest, isExist := c.Get("API_REQUEST")
if isExist {
var ok bool
apiRequestBody, ok = apiRequest.([]byte)
if !ok {
apiRequestBody = nil
}
}
var apiResponseBody []byte
apiResponse, isExist := c.Get("API_RESPONSE")
if isExist {
var ok bool
apiResponseBody, ok = apiResponse.([]byte)
if !ok {
apiResponseBody = nil
}
}
// Log complete non-streaming response
return w.logger.LogRequest(
w.requestInfo.URL,
@@ -180,6 +200,8 @@ func (w *ResponseWriterWrapper) Finalize() error {
finalStatusCode,
finalHeaders,
w.body.Bytes(),
apiRequestBody,
apiResponseBody,
)
}