From 3e936a3c2f80ff83b96672d6474b2358499ade44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Tue, 7 Aug 2018 11:13:25 +0200 Subject: [PATCH] output file name sequence fix --- main.go | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 430e6ec..cf5a807 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,7 @@ type Logger struct { outputFile *os.File outputFileName string + fullName string maxFileSize int64 splitCount int @@ -47,8 +48,9 @@ func New(name string, maxFileSize int64) (logger *Logger, err error) { } date := strings.Replace(time.Now().Format(time.RFC3339), ":", ".", -1) - logger.outputFileName += "_" + date + ".txt" - path := filepath.Join(logDirName, logger.outputFileName) + logger.outputFileName += "_" + date + logger.fullName = logger.outputFileName + ".txt" + path := filepath.Join(logDirName, logger.fullName) logger.outputFile, err = os.Create(path) if err != nil { fmt.Printf("logger: new: couldn't create event log file\n") @@ -94,11 +96,24 @@ func (l *Logger) RequestLog(req *http.Request, userid string) string { 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) + 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 { - b.WriteString(string(body)) + 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") @@ -184,7 +199,7 @@ func (l *Logger) split() { l.splitCount++ // open new file var errnew error - path := filepath.Join(logDirName, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)) + path := filepath.Join(logDirName, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)+".txt") l.outputFile, errnew = os.Create(path) if errnew != nil { -- 1.8.1.2