From df74f4a7ee0b8df78da5b96abc0749b19854ddd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Thu, 5 Apr 2018 13:40:59 +0200 Subject: [PATCH] new filename format --- main.go | 95 ++++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 34 deletions(-) diff --git a/main.go b/main.go index 6318371..cefcd94 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,8 @@ package gologger import ( "fmt" + "io/ioutil" + "net/http" "os" "path/filepath" "runtime" @@ -25,6 +27,8 @@ type Logger struct { outputFileName string maxFileSize int64 + + splitCount int } func New(name string, maxFileSize int64) (logger *Logger, err error) { @@ -42,9 +46,9 @@ func New(name string, maxFileSize int64) (logger *Logger, err error) { } } - date := strings.Split(time.Now().Format(time.RFC3339), "T")[0] - timestamp := "_" + date - path := filepath.Join(logDirName, logger.outputFileName+timestamp+".txt") + date := strings.Replace(time.Now().Format(time.RFC3339), ":", ".", -1) + logger.outputFileName += "_" + date + ".txt" + path := filepath.Join(logDirName, logger.outputFileName) logger.outputFile, err = os.Create(path) if err != nil { fmt.Printf("logger: new: couldn't create event log file\n") @@ -54,45 +58,67 @@ func New(name string, maxFileSize int64) (logger *Logger, err error) { return logger, nil } -func (logger *Logger) Print(format string, v ...interface{}) { +func (l *Logger) Print(format string, v ...interface{}) { msg := fmt.Sprintf(format, v...) fmt.Printf(time.Now().Format(time.RFC3339) + ": " + msg + "\n") } -func (logger *Logger) Log(format string, v ...interface{}) { - if logger.outputFile != nil { - logger.mu.Lock() - defer logger.mu.Unlock() +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 logger.shouldSplit(len(s)) { - logger.split() + if l.shouldSplit(len(s)) { + l.split() } - logger.outputFile.WriteString(s) + l.outputFile.WriteString(s) } } -// TODO(marko) -/* -func (logger *Logger) LogHTTPRequest(req *http.Request) { - if logger.outputFile != nil { - logger.mu.Lock() - defer logger.mu.Unlock() +func (l *Logger) LogRequest(req *http.Request, userid string) { + if l.outputFile != nil { + if userid == "" { + userid = "-" + } - msg := fmt.Sprintf(format, v...) - if logger.shouldSplit(len(s)) { - logger.split() + var b strings.Builder + // CLF-like header + ts := time.Now().Format(time.RFC3339) + fmt.Fprintf(&b, "%s - %s [%s] \"%s %s\" %s\n", req.RemoteAddr, userid, ts, req.Method, req.URL.Path, req.Proto) + + b.WriteString("\nURL encoded:\n") + for k, v := range req.Form { + fmt.Fprintf(&b, "%s = %s\n", k, v) + } + + b.WriteString("\nBody:\n") + buf, err := ioutil.ReadAll(req.Body) + if err == nil { + b.WriteString(string(buf)) + } else { + fmt.Fprintf(os.Stderr, "%v\n", err) + } + b.WriteString("\n\n") + + msg := b.String() + //fmt.Println(msg) + + l.mu.Lock() + defer l.mu.Unlock() + + if l.shouldSplit(len(msg)) { + l.split() } - logger.outputFile.WriteString(time.Now().Format(time.RFC3339) + ": " + msg + "\n") + l.outputFile.WriteString(msg) } } -*/ -func (logger *Logger) Trace(format string, v ...interface{}) { - if logger.outputFile != nil { - logger.mu.Lock() - defer logger.mu.Unlock() +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 := "" @@ -102,16 +128,16 @@ func (logger *Logger) Trace(format string, v ...interface{}) { } else { s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + msg + "\n") } - if logger.shouldSplit(len(s)) { - logger.split() + if l.shouldSplit(len(s)) { + l.split() } - logger.outputFile.WriteString(s) + l.outputFile.WriteString(s) } } -func (logger *Logger) Close() { - if logger.outputFile != nil { - err := logger.outputFile.Close() +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") } @@ -119,16 +145,17 @@ func (logger *Logger) Close() { } func (l *Logger) split() { - timestamp := "_" + time.Now().Format(time.RFC3339) // close old file err := l.outputFile.Close() if err != nil { fmt.Printf("logger: split: couldn't close event file\n") return } + + l.splitCount++ // open new file var errnew error - path := filepath.Join(logDirName, l.outputFileName+timestamp) + path := filepath.Join(logDirName, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)) l.outputFile, errnew = os.Create(path) if errnew != nil { -- 1.8.1.2