Commit 3ba7b569571862cf5a7235d0b4aa4c43185c77e7
1 parent
f201b05f1c
Exists in
master
added mutex locks for event and error files
Showing
1 changed file
with
15 additions
and
1 deletions
Show diff stats
gologger.go
1 | +// @TODO: Implement mutex lock for files access. | |
1 | 2 | package gologger |
2 | 3 | |
3 | 4 | import ( |
... | ... | @@ -5,8 +6,12 @@ import ( |
5 | 6 | "os" |
6 | 7 | "time" |
7 | 8 | "strconv" |
9 | + "sync" | |
8 | 10 | ) |
9 | 11 | |
12 | +var muEv = &sync.Mutex{} | |
13 | +var muEr = &sync.Mutex{} | |
14 | + | |
10 | 15 | const MaxLogFileSize5MB int64 = 5*1024*1024 |
11 | 16 | const MaxLogFileSize1MB int64 = 1*1024*1024 |
12 | 17 | const MaxLogFileSize500KB int64 = 500*1024 |
... | ... | @@ -85,7 +90,9 @@ func (l *Logger) Close() { |
85 | 90 | func (l *Logger) splitEventLog() { |
86 | 91 | timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10) |
87 | 92 | |
88 | - // events | |
93 | + muEv.Lock() | |
94 | + defer muEv.Unlock() | |
95 | + | |
89 | 96 | evfstats, _ := l.eventf.Stat() |
90 | 97 | if evfstats.Size() >= MaxLogFileSize100KB { |
91 | 98 | // close old file |
... | ... | @@ -108,6 +115,9 @@ func (l *Logger) splitEventLog() { |
108 | 115 | func (l *Logger) splitErrorLog() { |
109 | 116 | timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10) |
110 | 117 | |
118 | + muEr.Lock() | |
119 | + defer muEr.Unlock() | |
120 | + | |
111 | 121 | erfstats, _ := l.errorf.Stat() |
112 | 122 | if erfstats.Size() >= MaxLogFileSize100KB { |
113 | 123 | // close old file |
... | ... | @@ -128,11 +138,15 @@ func (l *Logger) splitErrorLog() { |
128 | 138 | } |
129 | 139 | |
130 | 140 | func (l *Logger) LogEvent(event string) { |
141 | + muEv.Lock() | |
142 | + defer muEv.Unlock() | |
131 | 143 | l.eventf.WriteString(time.Now().Format(time.UnixDate) + ": " + event + "\n") |
132 | 144 | l.splitEventLog() |
133 | 145 | } |
134 | 146 | |
135 | 147 | func (l *Logger) LogError(err error) { |
148 | + muEr.Lock() | |
149 | + defer muEr.Unlock() | |
136 | 150 | l.errorf.WriteString(time.Now().Format(time.UnixDate) + ": " + err.Error() + "\n") |
137 | 151 | l.splitErrorLog() |
138 | 152 | } | ... | ... |