Commit a7081e581392838008eb009222c7692981ff4634

Authored by Marko Tikvić
1 parent b45f88e17f
Exists in master

pretty print json

Showing 1 changed file with 18 additions and 1 deletions   Show diff stats
1 1 package gologger
2 2  
3 3 import (
  4 + "bytes"
  5 + "encoding/json"
4 6 "fmt"
5 7 "net/http"
6 8 "net/http/httputil"
... ... @@ -137,7 +139,16 @@ func (l *Logger) LogHTTPRequest(req *http.Request, userID string) string {
137 139 if err != nil {
138 140 fmt.Fprintf(os.Stderr, "%v\n", err)
139 141 }
140   - b.WriteString(string(body) + "\n\n")
  142 +
  143 + const sepStr = "\r\n\r\n"
  144 + sepIndex := bytes.Index(body, []byte(sepStr))
  145 + if sepIndex == -1 {
  146 + b.WriteString(string(body) + "\n\n")
  147 + } else {
  148 + sepIndex += len(sepStr)
  149 + payload, _ := printJSON(body[sepIndex:])
  150 + b.WriteString(string(body[:sepIndex]) + string(payload) + "\n\n")
  151 + }
141 152  
142 153 return b.String()
143 154 }
... ... @@ -204,3 +215,9 @@ func (l *Logger) shouldSplit(nextEntrySize int) bool {
204 215 stats, _ := l.outputFile.Stat()
205 216 return int64(nextEntrySize) >= (l.maxFileSize - stats.Size())
206 217 }
  218 +
  219 +func printJSON(in []byte) (out []byte, err error) {
  220 + var buf bytes.Buffer
  221 + err = json.Indent(&buf, in, "", " ")
  222 + return buf.Bytes(), err
  223 +}
... ...