Skip to content

Commit

Permalink
[AT-40][Add] Add pretty print json call standard function for REQ/RESP
Browse files Browse the repository at this point in the history
  • Loading branch information
martinstibbe committed Jul 13, 2020
1 parent 624154a commit dd48940
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions edgegrid/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package edgegrid

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
Expand Down Expand Up @@ -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))
}
}

Expand All @@ -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))
}
}

Expand All @@ -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))
}
}

Expand All @@ -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")
}

0 comments on commit dd48940

Please sign in to comment.