From 372beaf62734908b8eec30fdc1dd7f76774ffee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Fri, 15 Jun 2018 11:16:14 +0200 Subject: [PATCH] refactored if statements --- main.go | 159 +++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 86 insertions(+), 73 deletions(-) diff --git a/main.go b/main.go index 841373b..0a8b255 100644 --- a/main.go +++ b/main.go @@ -64,103 +64,116 @@ func (l *Logger) Print(format string, v ...interface{}) { } func (l *Logger) Log(format string, v ...interface{}) { - if l.outputFile != nil { - l.mu.Lock() - defer l.mu.Unlock() - - msg := fmt.Sprintf(format, v...) - s := time.Now().Format(time.RFC3339) + ": " + msg + "\n" - if l.shouldSplit(len(s)) { - l.split() - } - l.outputFile.WriteString(s) + if l.outputFile == nil { + return + } + + l.mu.Lock() + defer l.mu.Unlock() + + msg := fmt.Sprintf(format, v...) + s := time.Now().Format(time.RFC3339) + ": " + msg + "\n" + if l.shouldSplit(len(s)) { + l.split() } + l.outputFile.WriteString(s) } -func (l *Logger) LogRequest(req *http.Request, userid string) { - if l.outputFile != nil { - if userid == "" { - userid = "-" - } +func (l *Logger) RequestLog(req *http.Request, userid string) string { + if l.outputFile == nil { + return "" + } - var b strings.Builder - b.WriteString("Request:\n") - // CLF-like header - ts := time.Now().Format(time.RFC3339) - fmt.Fprintf(&b, "%s %s\n", req.RemoteAddr, ts) - - body, err := httputil.DumpRequest(req, true) - if err != nil { - fmt.Fprintf(os.Stderr, "%v\n", err) - } else { - b.WriteString(string(body)) - } - b.WriteString("\n") + if userid == "" { + userid = "-" + } - msg := b.String() + var b strings.Builder + b.WriteString("Request:\n") + // CLF-like header + ts := time.Now().Format(time.RFC3339) + fmt.Fprintf(&b, "%s %s\n", req.RemoteAddr, ts) - l.mu.Lock() - defer l.mu.Unlock() + 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") - if l.shouldSplit(len(msg)) { - l.split() - } - l.outputFile.WriteString(msg) + msg := b.String() + + return msg +} + +func (l *Logger) ResponseLog(status int, duration time.Duration) string { + if l.outputFile == nil { + return "" } + + var b strings.Builder + fmt.Fprintf(&b, "Response: %d %v\n", status, duration) + b.WriteString("==============================================================\n\n") + msg := b.String() + + return msg } -func (l *Logger) LogResponse(w http.ResponseWriter, req *http.Request, duration time.Duration) { - if l.outputFile != nil { - var b strings.Builder - b.WriteString("Response:\n") - fmt.Fprintf(&b, "%s %s\n", req.Method, req.RequestURI) - for k, v := range w.Header() { - fmt.Fprintf(&b, "%s: %s\n", k, v) - } - fmt.Fprintf(&b, "\nCompleted in: %v\n", duration) - b.WriteString("==============================================================\n\n") - msg := b.String() +func (l *Logger) LogHTTPTraffic(in string, out string) { + if l.outputFile == nil { + return + } - l.mu.Lock() - defer l.mu.Unlock() + msg := in + out - if l.shouldSplit(len(msg)) { - l.split() - } - l.outputFile.WriteString(msg) + l.mu.Lock() + defer l.mu.Unlock() + + if l.shouldSplit(len(msg)) { + l.split() } + l.outputFile.WriteString(msg) } func (l *Logger) Trace(format string, v ...interface{}) { - if l.outputFile != nil { - l.mu.Lock() - defer l.mu.Unlock() - _, file, line, ok := runtime.Caller(1) - - s := "" - msg := fmt.Sprintf(format, v...) - if ok { - s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg) - } else { - s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + msg + "\n") - } - if l.shouldSplit(len(s)) { - l.split() - } - l.outputFile.WriteString(s) + if l.outputFile == nil { + return + } + + l.mu.Lock() + defer l.mu.Unlock() + _, file, line, ok := runtime.Caller(1) + + s := "" + msg := fmt.Sprintf(format, v...) + if ok { + s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg) + } else { + s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + msg + "\n") } + if l.shouldSplit(len(s)) { + l.split() + } + l.outputFile.WriteString(s) } func (l *Logger) Close() { - if l.outputFile != nil { - err := l.outputFile.Close() - if err != nil { - fmt.Printf("logger: on exit: couldn't close event log file\n") - } + if l.outputFile == nil { + return + } + + err := l.outputFile.Close() + if err != nil { + fmt.Printf("logger: on exit: couldn't close event log file\n") } } func (l *Logger) split() { + if l.outputFile == nil { + return + } + // close old file err := l.outputFile.Close() if err != nil { -- 1.8.1.2