diff --git a/edgegrid/log.go b/edgegrid/log.go index 1af021b6..1b98517b 100644 --- a/edgegrid/log.go +++ b/edgegrid/log.go @@ -16,6 +16,8 @@ package edgegrid import ( "bufio" + "bytes" + "encoding/json" "fmt" "net/http" "net/http/httputil" @@ -105,6 +107,7 @@ func PrintHttpRequestCorrelation(req *http.Request, body bool, correlationid str b, err := httputil.DumpRequestOut(req, body) if err == nil { LogMultiline(EdgegridLog.Traceln, string(b)) + PrintfCorrelation("[DEBUG] REQUEST", correlationid, prettyPrintJsonLines(b)) } } @@ -117,7 +120,6 @@ func PrintHttpResponse(res *http.Response, body bool) { b, err := httputil.DumpResponse(res, body) if err == nil { LogMultiline(EdgegridLog.Traceln, string(b)) - logstd.Printf("[DEBUG] RESPONSE %s\n", string(b)) } } @@ -129,7 +131,7 @@ func PrintHttpResponseCorrelation(res *http.Response, body bool, correlationid s b, err := httputil.DumpResponse(res, body) if err == nil { LogMultiline(EdgegridLog.Traceln, string(b)) - logstd.Printf("[DEBUG]%v RESPONSE %s\n", correlationid, string(b)) + PrintfCorrelation("[DEBUG] RESPONSE ", correlationid, prettyPrintJsonLines(b)) } } @@ -143,3 +145,17 @@ func PrintfCorrelation(level string, correlationid string, msg string) { } } + +// prettyPrintJsonLines iterates through a []byte line-by-line, +// transforming any lines that are complete json into pretty-printed json. +func prettyPrintJsonLines(b []byte) string { + parts := strings.Split(string(b), "\n") + for i, p := range parts { + if b := []byte(p); json.Valid(b) { + var out bytes.Buffer + json.Indent(&out, b, "", " ") + parts[i] = out.String() + } + } + return strings.Join(parts, "\n") +}