@@ -28,13 +28,14 @@ import (
28
28
)
29
29
30
30
type 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"`
38
39
}
39
40
40
41
type LoggingMiddlewareImpl struct {
@@ -72,22 +73,27 @@ func (impl LoggingMiddlewareImpl) LoggingMiddleware(next http.Handler) http.Hand
72
73
// Restore the request body for downstream handlers
73
74
r .Body = io .NopCloser (& bodyBuffer )
74
75
76
+ // Record start time for calculating response time
77
+ startTime := time .Now ()
78
+
75
79
auditLogDto := & AuditLoggerDTO {
76
80
UrlPath : r .URL .Path ,
77
81
UserEmail : userEmail ,
78
- UpdatedOn : time . Now () ,
82
+ UpdatedOn : startTime ,
79
83
QueryParams : r .URL .Query ().Encode (),
80
84
RequestPayload : bodyBuffer .Bytes (),
81
85
RequestMethod : r .Method ,
82
86
}
83
87
// Call the next handler in the chain.
84
88
next .ServeHTTP (d , r )
85
89
90
+ // Calculate response time
91
+ auditLogDto .ResponseTime = time .Since (startTime )
86
92
auditLogDto .ApiResponseCode = d .Status ()
87
93
LogRequest (auditLogDto )
88
94
})
89
95
}
90
96
91
97
func 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 )
93
99
}
0 commit comments