From 093191eb2230cc24d6d609f6307b8681d9baae3a Mon Sep 17 00:00:00 2001 From: "marko.tikvic" Date: Wed, 6 Feb 2019 10:47:40 +0100 Subject: [PATCH] much cleaner, much better --- main.go | 58 ++++++++++++++-------------------------------------------- 1 file changed, 14 insertions(+), 44 deletions(-) diff --git a/main.go b/main.go index 0c92dc1..b7c3bdd 100644 --- a/main.go +++ b/main.go @@ -81,70 +81,40 @@ func (l *Logger) Log(format string, v ...interface{}) { l.outputFile.WriteString(s) } -func (l *Logger) RequestLog(req *http.Request, userID string) string { - if l.outputFile == nil { - return "" - } - +func (l *Logger) LogHTTPRequest(req *http.Request, userID string) string { if userID == "" { userID = "-" } var b strings.Builder + b.WriteString("Request:\n") // CLF-like header - ts := time.Now().Format(time.RFC3339) - fmt.Fprintf(&b, "%s %s %s\n", req.RemoteAddr, userID, ts) - - headers := req.Header - content := headers.Get("Content-Type") - if content != "application/json" { - b.WriteString(fmt.Sprintf("%s %s\n", req.Method, req.URL)) - for k, h := range headers { - b.WriteString(k + ": ") - for _, h2 := range h { - b.WriteString(h2 + " ") - } - b.WriteString("\n") - } - } else { - body, err := httputil.DumpRequest(req, true) - if err != nil { - fmt.Fprintf(os.Stderr, "%v\n", err) - } else { - b.WriteString(string(body)) - } - } - b.WriteString("\n\n") - - msg := b.String() + fmt.Fprintf(&b, "%s %s %s\n", req.RemoteAddr, userID, time.Now().Format(time.RFC3339)) - return msg -} - -func (l *Logger) ResponseLog(status int, duration time.Duration, size int64) string { - if l.outputFile == nil { - return "" + body, err := httputil.DumpRequest(req, true) + if err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) } + b.WriteString(string(body) + "\n\n") - var b strings.Builder - fmt.Fprintf(&b, "Response:\n%d %v %dB\n", status, duration, size) - b.WriteString("==============================================================\n") - msg := b.String() + return b.String() +} - return msg +func (l *Logger) LogHTTPResponse(status int, duration time.Duration, size int) string { + splitLine := "==============================================================\n" + return fmt.Sprintf("Response:\n%d %v %dB\n%s", status, duration, size, splitLine) } -func (l *Logger) LogHTTPTraffic(in string, out string) { +func (l *Logger) CombineHTTPLogs(in string, out string) { if l.outputFile == nil { return } - msg := in + out - l.mu.Lock() defer l.mu.Unlock() + msg := in + out if l.shouldSplit(len(msg)) { l.split() } -- 1.8.1.2