@@ -25,6 +25,7 @@ import (
2525
2626 "github.com/devtron-labs/devtron/internal/middleware"
2727 "github.com/devtron-labs/devtron/pkg/auth/user"
28+ "github.com/devtron-labs/devtron/util"
2829)
2930
3031type AuditLoggerDTO struct {
@@ -36,6 +37,7 @@ type AuditLoggerDTO struct {
3637 RequestPayload []byte `json:"requestPayload"`
3738 RequestMethod string `json:"requestMethod"`
3839 ResponseTime time.Duration `json:"responseTime"`
40+ ClientIp string `json:"clientIp"`
3941}
4042
4143type LoggingMiddlewareImpl struct {
@@ -56,13 +58,12 @@ type LoggingMiddleware interface {
5658func (impl LoggingMiddlewareImpl ) LoggingMiddleware (next http.Handler ) http.Handler {
5759 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
5860 d := middleware .NewDelegator (w , nil )
59-
6061 token := r .Header .Get ("token" )
6162 userEmail , err := impl .userService .GetEmailFromToken (token )
6263 if err != nil {
6364 log .Printf ("AUDIT_LOG: user does not exists" )
6465 }
65-
66+ clientIp := util . GetClientIP ( r )
6667 // Read the request body into a buffer
6768 var bodyBuffer bytes.Buffer
6869 _ , err = io .Copy (& bodyBuffer , r .Body )
@@ -83,6 +84,7 @@ func (impl LoggingMiddlewareImpl) LoggingMiddleware(next http.Handler) http.Hand
8384 QueryParams : r .URL .Query ().Encode (),
8485 RequestPayload : bodyBuffer .Bytes (),
8586 RequestMethod : r .Method ,
87+ ClientIp : clientIp ,
8688 }
8789 // Call the next handler in the chain.
8890 next .ServeHTTP (d , r )
@@ -95,5 +97,5 @@ func (impl LoggingMiddlewareImpl) LoggingMiddleware(next http.Handler) http.Hand
9597}
9698
9799func LogRequest (auditLogDto * AuditLoggerDTO ) {
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 )
100+ log .Printf ("AUDIT_LOG: clientIp: %s, requestMethod: %s, urlPath: %s, queryParams: %s, updatedBy: %s, updatedOn: %s, apiResponseCode: %d, responseTime: %s, requestPayload: %s" , auditLogDto . ClientIp , auditLogDto .RequestMethod , auditLogDto .UrlPath , auditLogDto .QueryParams , auditLogDto .UserEmail , auditLogDto .UpdatedOn , auditLogDto .ApiResponseCode , auditLogDto .ResponseTime , auditLogDto .RequestPayload )
99101}
0 commit comments