From a7081e581392838008eb009222c7692981ff4634 Mon Sep 17 00:00:00 2001 From: "marko.tikvic" Date: Wed, 3 Apr 2019 16:00:28 +0200 Subject: [PATCH] pretty print json --- main.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 6bc0f55..dfe162b 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,8 @@ package gologger import ( + "bytes" + "encoding/json" "fmt" "net/http" "net/http/httputil" @@ -137,7 +139,16 @@ func (l *Logger) LogHTTPRequest(req *http.Request, userID string) string { if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) } - b.WriteString(string(body) + "\n\n") + + const sepStr = "\r\n\r\n" + sepIndex := bytes.Index(body, []byte(sepStr)) + if sepIndex == -1 { + b.WriteString(string(body) + "\n\n") + } else { + sepIndex += len(sepStr) + payload, _ := printJSON(body[sepIndex:]) + b.WriteString(string(body[:sepIndex]) + string(payload) + "\n\n") + } return b.String() } @@ -204,3 +215,9 @@ func (l *Logger) shouldSplit(nextEntrySize int) bool { stats, _ := l.outputFile.Stat() return int64(nextEntrySize) >= (l.maxFileSize - stats.Size()) } + +func printJSON(in []byte) (out []byte, err error) { + var buf bytes.Buffer + err = json.Indent(&buf, in, "", " ") + return buf.Bytes(), err +} -- 1.8.1.2