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 +}