From a99c98307ce9ca0ac1244cdfd8773cab4e150ab1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Wed, 21 Sep 2016 14:06:20 +0200 Subject: [PATCH] first commit --- gologger.go | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 gologger.go diff --git a/gologger.go b/gologger.go new file mode 100644 index 0000000..f835d36 --- /dev/null +++ b/gologger.go @@ -0,0 +1,140 @@ +package gologger + +import ( + "fmt" + "os" + "time" + "strings" + "strconv" +) + +const MaxLogFileSize5MB int64 = 5*1024*1024 +const MaxLogFileSize1MB int64 = 1*1024*1024 +const MaxLogFileSize500KB int64 = 500*1024 +const MaxLogFileSize100KB int64 = 100*1024 +const MaxLogFileSize512B int64 = 512 + +const ErrDirName = "error-logs" +const EvtDirName = "event-logs" + +type Logger struct { + eventf *os.File + eventFileName string + errorf *os.File + errorFileName string +} + +func NewLogger(eventFileName, errorFileName string) (*Logger, error) { + var l Logger + var err error + + timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10) + + // create files + l.eventFileName = eventFileName + l.errorFileName = errorFileName + + l.eventf, err = os.Create("./" + EvtDirName + "/" + + eventFileName + timestamp + ".txt") + + if err != nil { + fmt.Printf("logger: new: couldn't create event log file\n") + return &l, err + } + + l.errorf, err = os.Create("./" + ErrDirName + "/" + + errorFileName + timestamp + ".txt") + + if err != nil { + fmt.Printf("logger: new: couldn't create error log file\n") + return &l, err + } + + // create directories to keep log files + 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 &l, err + } + } + + 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 &l, err + } + } + + return &l, nil +} + +func (l *Logger) Close() { + // close files + err := l.eventf.Close() + if err != nil { + fmt.Printf("logger: on exit: couldn't close event log file\n") + } + + err = l.errorf.Close() + if err != nil { + fmt.Printf("logger: on exit: couldn't close error log file\n") + } +} + +func (l *Logger) splitEventLog() { + timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10) + + // events + evfstats, _ := l.eventf.Stat() + if evfstats.Size() >= MaxLogFileSize100KB { + // 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 + l.eventf, errnew = os.Create("./" + EvtDirName + "/" + + l.eventFileName + timestamp + ".txt") + + if errnew != nil { + fmt.Printf("logger: split: couldn't create event log file\n") + } + } +} + +func (l *Logger) splitErrorLog() { + timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10) + + erfstats, _ := l.errorf.Stat() + if erfstats.Size() >= MaxLogFileSize100KB { + // 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 + l.errorf, errnew = os.Create("./" + ErrDirName + "/" + + l.errorFileName + timestamp + ".txt") + + if errnew != nil { + fmt.Printf("logger: split: couldn't create error log file\n") + } + } +} + +func (l *Logger) LogEvent(event string) { + l.eventf.WriteString(time.Now().Format(time.UnixDate) + ": " + event + "\n") + l.splitEventLog() +} + +func (l *Logger) LogError(err error) { + estring := strings.TrimSuffix(fmt.Sprintf("%v", err), "\n") + l.errorf.WriteString(time.Now().Format(time.UnixDate) + ": " + estring + "\n") + l.splitErrorLog() +} -- 1.8.1.2