From 98cf2ceebd0d095ad926538279e57a3612a0898e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Mon, 26 Feb 2018 11:10:46 +0100 Subject: [PATCH] new interaface; allow multiple logger instances --- main.go | 218 ++++++++++++++++++++++------------------------------------------ 1 file changed, 75 insertions(+), 143 deletions(-) diff --git a/main.go b/main.go index 3e9b27b..4472132 100644 --- a/main.go +++ b/main.go @@ -9,200 +9,132 @@ import ( "time" ) -type Option uint8 - -const ( - Events Option = 0x01 - Errors Option = 0x02 -) - const ( MaxLogSize5MB int64 = 5 * 1024 * 1024 MaxLogSize1MB int64 = 1 * 1024 * 1024 MaxLogSize500KB int64 = 500 * 1024 MaxLogSize100KB int64 = 100 * 1024 MaxLogSize512B int64 = 512 + logDirName = "log" ) -const errDirName = "error-logs" -const evtDirName = "event-logs" - type Logger struct { - muEv *sync.Mutex - eventf *os.File - - eventFileName string + mu *sync.Mutex + outputFile *os.File - muEr *sync.Mutex - errorf *os.File - - errorFileName string - - splitSize int64 + outputFileName string + maxFileSize int64 } -func New(name string, flags Option, splitSize int64) (logger *Logger, err error) { +func New(name string, maxFileSize int64) (logger *Logger, err error) { logger = &Logger{} - logger.splitSize = splitSize - - timestamp := "_" + time.Now().Format(time.RFC3339) - - // event file/dir - if flags&Events > 0 { - err = os.Mkdir(evtDirName, os.ModePerm) - if err != nil { - if !os.IsExist(err) { - fmt.Printf("logger: mkdir: couldn't create event log directory\n") - return nil, err - } - } + logger.outputFileName = name + "-log" + logger.mu = &sync.Mutex{} + logger.maxFileSize = maxFileSize - logger.eventFileName = name + "-events" - path := filepath.Join(evtDirName, logger.eventFileName+timestamp+".txt") - logger.eventf, err = os.Create(path) - if err != nil { - fmt.Printf("logger: new: couldn't create event log file\n") + err = os.Mkdir(logDirName, os.ModePerm) + if err != nil { + if !os.IsExist(err) { + fmt.Printf("logger: mkdir: couldn't create event log directory\n") return nil, err } - - logger.muEv = &sync.Mutex{} } - // error file/dir - if flags&Errors > 0 { - err = os.Mkdir(errDirName, os.ModePerm) - if err != nil { - if !os.IsExist(err) { - fmt.Printf("logger: new: couldn't create error log directory\n") - return nil, err - } - } - - logger.errorFileName = name + "-errors" - path := filepath.Join(errDirName, logger.errorFileName+timestamp+".txt") - logger.errorf, err = os.Create(path) - if err != nil { - fmt.Printf("logger: new: couldn't create error log file\n") - return nil, err - } - - logger.muEr = &sync.Mutex{} + timestamp := "_" + time.Now().Format(time.RFC3339) + path := filepath.Join(logDirName, logger.outputFileName+timestamp+".txt") + logger.outputFile, err = os.Create(path) + if err != nil { + fmt.Printf("logger: new: couldn't create event log file\n") + return nil, err } return logger, nil } -func (logger *Logger) Print(s string) { - fmt.Printf(time.Now().Format(time.RFC3339) + ": " + s + "\n") +func (logger *Logger) Print(format string, v ...interface{}) { + msg := fmt.Sprintf(format, v...) + fmt.Printf(time.Now().Format(time.RFC3339) + ": " + msg + "\n") } -func (logger *Logger) LogEvent(event string) { - if logger.eventf != nil { - logger.muEv.Lock() - defer logger.muEv.Unlock() - logger.eventf.WriteString(time.Now().Format(time.RFC3339) + ": " + event + "\n") - logger.splitEventLog() - } -} +func (logger *Logger) Log(format string, v ...interface{}) { + if logger.outputFile != nil { + logger.mu.Lock() + defer logger.mu.Unlock() -func (logger *Logger) LogError(comment string, err error) { - if logger.errorf != nil { - logger.muEr.Lock() - defer logger.muEr.Unlock() - logger.errorf.WriteString(time.Now().Format(time.RFC3339) + ": " + comment + ": " + err.Error() + "\n") - logger.splitErrorLog() + msg := fmt.Sprintf(format, v...) + s := time.Now().Format(time.RFC3339) + ": " + msg + "\n" + if logger.shouldSplit(len(s)) { + logger.split() + } + logger.outputFile.WriteString(s) } } -func (logger *Logger) TraceEvent(event string) { - if logger.eventf != nil { - logger.muEv.Lock() - defer logger.muEv.Unlock() - _, file, line, ok := runtime.Caller(1) - var s string - if ok { - s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, event) - } else { - s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + event + "\n") +// TODO(marko) +/* +func (logger *Logger) LogHTTPRequest(req *http.Request) { + if logger.outputFile != nil { + logger.mu.Lock() + defer logger.mu.Unlock() + + msg := fmt.Sprintf(format, v...) + if logger.shouldSplit(len(s)) { + logger.split() } - logger.eventf.WriteString(s) - logger.splitEventLog() + logger.outputFile.WriteString(time.Now().Format(time.RFC3339) + ": " + msg + "\n") } } +*/ -func (logger *Logger) TraceError(err error) { - if logger.errorf != nil { - logger.muEr.Lock() - defer logger.muEr.Unlock() +func (logger *Logger) Trace(format string, v ...interface{}) { + if logger.outputFile != nil { + logger.mu.Lock() + defer logger.mu.Unlock() _, file, line, ok := runtime.Caller(1) - var s string + + s := "" + msg := fmt.Sprintf(format, v...) if ok { - s = fmt.Sprintf("%s %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, err.Error()) + 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]:" + err.Error() + "\n") + s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + msg + "\n") + } + if logger.shouldSplit(len(s)) { + logger.split() } - logger.errorf.WriteString(s) - logger.splitErrorLog() + logger.outputFile.WriteString(s) } } func (logger *Logger) Close() { - if logger.eventf != nil { - err := logger.eventf.Close() + if logger.outputFile != nil { + err := logger.outputFile.Close() if err != nil { fmt.Printf("logger: on exit: couldn't close event log file\n") } } - - if logger.errorf != nil { - err := logger.errorf.Close() - if err != nil { - fmt.Printf("logger: on exit: couldn't close error log file\n") - } - } } -func (l *Logger) splitEventLog() { +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 + } + // open new file + var errnew error + path := filepath.Join(logDirName, l.outputFileName+timestamp) + l.outputFile, errnew = os.Create(path) - evfstats, _ := l.eventf.Stat() - if evfstats.Size() >= l.splitSize { - // close old file - err := l.eventf.Close() - if err != nil { - fmt.Printf("logger: split: couldn't close event file\n") - return - } - // open new file - var errnew error - path := filepath.Join(evtDirName, l.eventFileName+timestamp) - l.eventf, errnew = os.Create(path) - - if errnew != nil { - fmt.Printf("logger: split: couldn't create event log file\n") - } + if errnew != nil { + fmt.Printf("logger: split: couldn't create event log file\n") } } -func (l *Logger) splitErrorLog() { - timestamp := "_" + time.Now().Format(time.RFC3339) - - erfstats, _ := l.errorf.Stat() - if erfstats.Size() >= l.splitSize { - // close old file - err := l.errorf.Close() - if err != nil { - fmt.Printf("logger: split: couldn't close error file\n") - return - } - // open new file - var errnew error - path := filepath.Join(errDirName, l.errorFileName+timestamp) - l.errorf, errnew = os.Create(path) - - if errnew != nil { - fmt.Printf("logger: split: couldn't create error log file\n") - } - } +func (l *Logger) shouldSplit(nextEntrySize int) bool { + stats, _ := l.outputFile.Stat() + return int64(nextEntrySize) >= (l.maxFileSize - stats.Size()) } -- 1.8.1.2