diff --git a/go.mod b/go.mod index 64949b4..4bc1be7 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/cookielab/traefik-middleware-request-logger go 1.19 -require github.com/google/uuid v1.6.0 // indirect +require github.com/google/uuid v1.6.0 diff --git a/logger.go b/logger.go index ca947ad..f46b678 100644 --- a/logger.go +++ b/logger.go @@ -63,7 +63,7 @@ type requestData struct { URI string `json:"uri"` //nolint:tagliatelle Host string `json:"host"` //nolint:tagliatelle Headers map[string]string `json:"headers"` //nolint:tagliatelle - Body string `json:"body"` //nolint:tagliatelle + Body interface{} `json:"body"` //nolint:tagliatelle Verb string `json:"verb"` //nolint:tagliatelle IPAddress string `json:"ip_address"` //nolint:tagliatelle Time string `json:"time"` //nolint:tagliatelle @@ -74,7 +74,7 @@ type responseData struct { Time string `json:"time"` //nolint:tagliatelle Status int `json:"status"` //nolint:tagliatelle Headers map[string]string `json:"headers"` //nolint:tagliatelle - Body string `json:"body"` //nolint:tagliatelle + Body interface{} `json:"body"` //nolint:tagliatelle TransferEncoding string `json:"transfer_encoding"` //nolint:tagliatelle } @@ -252,14 +252,25 @@ func allowBodySize(bodySize, maxBodySize int) bool { return bodySize <= maxBodySize } -func allowedBody(body []byte, contentType string, maxBodySize int, contentTypes []string) string { +func allowedBody(body []byte, contentType string, maxBodySize int, contentTypes []string) interface{} { if len(body) == 0 { - return "" + return nil } - if allowBodySize(len(body), maxBodySize) && allowContentType(contentType, contentTypes) { - return string(body) + if !allowBodySize(len(body), maxBodySize) || !allowContentType(contentType, contentTypes) { + return fmt.Sprintf("Request body too large to log or wrong content type. Size: %d bytes, Content-type: %s", len(body), contentType) } - return fmt.Sprintf("Request body too large to log or wrong content type. Size: %d bytes, Content-type: %s", len(body), contentType) + + // Try to parse the body as JSON + var parsedBody interface{} + if contentType == "application/json" { + err := json.Unmarshal(body, &parsedBody) + if err == nil { + return parsedBody + } + } + + // If not JSON, return as string + return string(body) } func allowedHeader(headerName string, skipHeaders []string) bool {