Skip to content
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

feat(logger): update body types to interface for support parsed json #2

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 18 additions & 7 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
Loading