diff --git a/gologger.go b/gologger.go index 37737d3..62aee86 100644 --- a/gologger.go +++ b/gologger.go @@ -3,9 +3,9 @@ package gologger import ( "fmt" "os" + "runtime" "sync" "time" - "runtime" ) var muEv = &sync.Mutex{} @@ -27,30 +27,28 @@ type Logger struct { errorFileName string } -func NewLogger(eventFileName, errorFileName string) (*Logger, error) { - var l Logger - var err error +var logger Logger +func Init(eventFileName, errorFileName string) error { timestamp := "_" + time.Now().Format(time.RFC3339) + var err error // create files - l.eventFileName = eventFileName - l.errorFileName = errorFileName + logger.eventFileName = eventFileName + logger.errorFileName = errorFileName - l.eventf, err = os.Create("./" + EvtDirName + "/" + - eventFileName + timestamp + ".txt") + logger.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 + return err } - l.errorf, err = os.Create("./" + ErrDirName + "/" + - errorFileName + timestamp + ".txt") + logger.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 + return err } // create directories to keep log files @@ -58,7 +56,7 @@ func NewLogger(eventFileName, errorFileName string) (*Logger, error) { if err != nil { if !os.IsExist(err) { fmt.Printf("logger: mkdir: couldn't create event log directory\n") - return &l, err + return err } } @@ -66,21 +64,63 @@ func NewLogger(eventFileName, errorFileName string) (*Logger, error) { if err != nil { if !os.IsExist(err) { fmt.Printf("logger: new: couldn't create error log directory\n") - return &l, err + return err } } - return &l, nil + return nil +} + +func LogEvent(event string) { + muEv.Lock() + defer muEv.Unlock() + logger.eventf.WriteString(time.Now().Format(time.RFC3339) + ": " + event + "\n") + logger.splitEventLog() +} + +func LogError(comment string, err error) { + muEr.Lock() + defer muEr.Unlock() + logger.errorf.WriteString(time.Now().Format(time.RFC3339) + ": " + comment + ": " + err.Error() + "\n") + logger.splitErrorLog() +} + +func LogDetailedEvent(event string) { + muEv.Lock() + defer 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") + } + logger.eventf.WriteString(s) + logger.splitEventLog() +} + +func LogDetailedError(err error) { + muEr.Lock() + defer muEr.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, err.Error()) + } else { + s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + err.Error() + "\n") + } + logger.errorf.WriteString(s) + logger.splitErrorLog() } -func (l *Logger) Close() { +func Close() { // close files - err := l.eventf.Close() + err := logger.eventf.Close() if err != nil { fmt.Printf("logger: on exit: couldn't close event log file\n") } - err = l.errorf.Close() + err = logger.errorf.Close() if err != nil { fmt.Printf("logger: on exit: couldn't close error log file\n") } @@ -129,45 +169,3 @@ func (l *Logger) splitErrorLog() { } } } - -func (l *Logger) LogEvent(event string) { - muEv.Lock() - defer muEv.Unlock() - l.eventf.WriteString(time.Now().Format(time.RFC3339) + ": " + event + "\n") - l.splitEventLog() -} - -func (l *Logger) LogError(comment string, err error) { - muEr.Lock() - defer muEr.Unlock() - l.errorf.WriteString(time.Now().Format(time.RFC3339) + ": " + comment + ": " + err.Error() + "\n") - l.splitErrorLog() -} - -func (l *Logger) LogDetailedEvent(event string) { - muEv.Lock() - defer 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") - } - l.eventf.WriteString(s) - l.splitEventLog() -} - -func (l *Logger) LogDetailedError(err error) { - muEr.Lock() - defer muEr.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, err.Error()) - } else { - s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + err.Error() + "\n") - } - l.errorf.WriteString(s) - l.splitErrorLog() -}