@@ -28,13 +28,14 @@ import (
2828)
2929
3030type AuditLoggerDTO struct {
31- UrlPath string `json:"urlPath"`
32- UserEmail string `json:"userEmail"`
33- UpdatedOn time.Time `json:"updatedOn"`
34- QueryParams string `json:"queryParams"`
35- ApiResponseCode int `json:"apiResponseCode"`
36- RequestPayload []byte `json:"requestPayload"`
37- RequestMethod string `json:"requestMethod"`
31+ UrlPath string `json:"urlPath"`
32+ UserEmail string `json:"userEmail"`
33+ UpdatedOn time.Time `json:"updatedOn"`
34+ QueryParams string `json:"queryParams"`
35+ ApiResponseCode int `json:"apiResponseCode"`
36+ RequestPayload []byte `json:"requestPayload"`
37+ RequestMethod string `json:"requestMethod"`
38+ ResponseTime time.Duration `json:"responseTime"`
3839}
3940
4041type LoggingMiddlewareImpl struct {
@@ -72,22 +73,27 @@ func (impl LoggingMiddlewareImpl) LoggingMiddleware(next http.Handler) http.Hand
7273 // Restore the request body for downstream handlers
7374 r .Body = io .NopCloser (& bodyBuffer )
7475
76+ // Record start time for calculating response time
77+ startTime := time .Now ()
78+
7579 auditLogDto := & AuditLoggerDTO {
7680 UrlPath : r .URL .Path ,
7781 UserEmail : userEmail ,
78- UpdatedOn : time . Now () ,
82+ UpdatedOn : startTime ,
7983 QueryParams : r .URL .Query ().Encode (),
8084 RequestPayload : bodyBuffer .Bytes (),
8185 RequestMethod : r .Method ,
8286 }
8387 // Call the next handler in the chain.
8488 next .ServeHTTP (d , r )
8589
90+ // Calculate response time
91+ auditLogDto .ResponseTime = time .Since (startTime )
8692 auditLogDto .ApiResponseCode = d .Status ()
8793 LogRequest (auditLogDto )
8894 })
8995}
9096
9197func LogRequest (auditLogDto * AuditLoggerDTO ) {
92- 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 )
98+ 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 )
9399}
0 commit comments