From 3ba7b569571862cf5a7235d0b4aa4c43185c77e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Mon, 28 Nov 2016 10:16:04 +0100 Subject: [PATCH] added mutex locks for event and error files --- gologger.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/gologger.go b/gologger.go index e0fdc34..004ce13 100644 --- a/gologger.go +++ b/gologger.go @@ -1,3 +1,4 @@ +// @TODO: Implement mutex lock for files access. package gologger import ( @@ -5,8 +6,12 @@ import ( "os" "time" "strconv" + "sync" ) +var muEv = &sync.Mutex{} +var muEr = &sync.Mutex{} + const MaxLogFileSize5MB int64 = 5*1024*1024 const MaxLogFileSize1MB int64 = 1*1024*1024 const MaxLogFileSize500KB int64 = 500*1024 @@ -85,7 +90,9 @@ func (l *Logger) Close() { func (l *Logger) splitEventLog() { timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10) - // events + muEv.Lock() + defer muEv.Unlock() + evfstats, _ := l.eventf.Stat() if evfstats.Size() >= MaxLogFileSize100KB { // close old file @@ -108,6 +115,9 @@ func (l *Logger) splitEventLog() { func (l *Logger) splitErrorLog() { timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10) + muEr.Lock() + defer muEr.Unlock() + erfstats, _ := l.errorf.Stat() if erfstats.Size() >= MaxLogFileSize100KB { // close old file @@ -128,11 +138,15 @@ func (l *Logger) splitErrorLog() { } func (l *Logger) LogEvent(event string) { + muEv.Lock() + defer muEv.Unlock() l.eventf.WriteString(time.Now().Format(time.UnixDate) + ": " + event + "\n") l.splitEventLog() } func (l *Logger) LogError(err error) { + muEr.Lock() + defer muEr.Unlock() l.errorf.WriteString(time.Now().Format(time.UnixDate) + ": " + err.Error() + "\n") l.splitErrorLog() } -- 1.8.1.2