Skip to content

misc: append response time in audit log #6566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions api/util/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ import (
)

type AuditLoggerDTO struct {
UrlPath string `json:"urlPath"`
UserEmail string `json:"userEmail"`
UpdatedOn time.Time `json:"updatedOn"`
QueryParams string `json:"queryParams"`
ApiResponseCode int `json:"apiResponseCode"`
RequestPayload []byte `json:"requestPayload"`
RequestMethod string `json:"requestMethod"`
UrlPath string `json:"urlPath"`
UserEmail string `json:"userEmail"`
UpdatedOn time.Time `json:"updatedOn"`
QueryParams string `json:"queryParams"`
ApiResponseCode int `json:"apiResponseCode"`
RequestPayload []byte `json:"requestPayload"`
RequestMethod string `json:"requestMethod"`
ResponseTime time.Duration `json:"responseTime"`
}

type LoggingMiddlewareImpl struct {
Expand Down Expand Up @@ -72,22 +73,27 @@ func (impl LoggingMiddlewareImpl) LoggingMiddleware(next http.Handler) http.Hand
// Restore the request body for downstream handlers
r.Body = io.NopCloser(&bodyBuffer)

// Record start time for calculating response time
startTime := time.Now()

auditLogDto := &AuditLoggerDTO{
UrlPath: r.URL.Path,
UserEmail: userEmail,
UpdatedOn: time.Now(),
UpdatedOn: startTime,
QueryParams: r.URL.Query().Encode(),
RequestPayload: bodyBuffer.Bytes(),
RequestMethod: r.Method,
}
// Call the next handler in the chain.
next.ServeHTTP(d, r)

// Calculate response time
auditLogDto.ResponseTime = time.Since(startTime)
auditLogDto.ApiResponseCode = d.Status()
LogRequest(auditLogDto)
})
}

func LogRequest(auditLogDto *AuditLoggerDTO) {
log.Printf("AUDIT_LOG: requestMethod: %s, urlPath: %s, queryParams: %s, updatedBy: %s, updatedOn: %s, apiResponseCode: %d, requestPayload: %s", auditLogDto.RequestMethod, auditLogDto.UrlPath, auditLogDto.QueryParams, auditLogDto.UserEmail, auditLogDto.UpdatedOn, auditLogDto.ApiResponseCode, auditLogDto.RequestPayload)
log.Printf("AUDIT_LOG: requestMethod: %s, urlPath: %s, queryParams: %s, updatedBy: %s, updatedOn: %s, apiResponseCode: %d, responseTime: %s, requestPayload: %s", auditLogDto.RequestMethod, auditLogDto.UrlPath, auditLogDto.QueryParams, auditLogDto.UserEmail, auditLogDto.UpdatedOn, auditLogDto.ApiResponseCode, auditLogDto.ResponseTime, auditLogDto.RequestPayload)
}
Loading