Commit 3ba7b569571862cf5a7235d0b4aa4c43185c77e7

Authored by Marko Tikvić
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
1 // @TODO: Implement mutex lock for files access.
1 package gologger 2 package gologger
2 3
3 import ( 4 import (
4 "fmt" 5 "fmt"
5 "os" 6 "os"
6 "time" 7 "time"
7 "strconv" 8 "strconv"
9 "sync"
8 ) 10 )
9 11
12 var muEv = &sync.Mutex{}
13 var muEr = &sync.Mutex{}
14
10 const MaxLogFileSize5MB int64 = 5*1024*1024 15 const MaxLogFileSize5MB int64 = 5*1024*1024
11 const MaxLogFileSize1MB int64 = 1*1024*1024 16 const MaxLogFileSize1MB int64 = 1*1024*1024
12 const MaxLogFileSize500KB int64 = 500*1024 17 const MaxLogFileSize500KB int64 = 500*1024
13 const MaxLogFileSize100KB int64 = 100*1024 18 const MaxLogFileSize100KB int64 = 100*1024
14 const MaxLogFileSize512B int64 = 512 19 const MaxLogFileSize512B int64 = 512
15 20
16 const ErrDirName = "error-logs" 21 const ErrDirName = "error-logs"
17 const EvtDirName = "event-logs" 22 const EvtDirName = "event-logs"
18 23
19 type Logger struct { 24 type Logger struct {
20 eventf *os.File 25 eventf *os.File
21 eventFileName string 26 eventFileName string
22 errorf *os.File 27 errorf *os.File
23 errorFileName string 28 errorFileName string
24 } 29 }
25 30
26 func NewLogger(eventFileName, errorFileName string) (*Logger, error) { 31 func NewLogger(eventFileName, errorFileName string) (*Logger, error) {
27 var l Logger 32 var l Logger
28 var err error 33 var err error
29 34
30 timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10) 35 timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10)
31 36
32 // create files 37 // create files
33 l.eventFileName = eventFileName 38 l.eventFileName = eventFileName
34 l.errorFileName = errorFileName 39 l.errorFileName = errorFileName
35 40
36 l.eventf, err = os.Create("./" + EvtDirName + "/" + 41 l.eventf, err = os.Create("./" + EvtDirName + "/" +
37 eventFileName + timestamp + ".txt") 42 eventFileName + timestamp + ".txt")
38 43
39 if err != nil { 44 if err != nil {
40 fmt.Printf("logger: new: couldn't create event log file\n") 45 fmt.Printf("logger: new: couldn't create event log file\n")
41 return &l, err 46 return &l, err
42 } 47 }
43 48
44 l.errorf, err = os.Create("./" + ErrDirName + "/" + 49 l.errorf, err = os.Create("./" + ErrDirName + "/" +
45 errorFileName + timestamp + ".txt") 50 errorFileName + timestamp + ".txt")
46 51
47 if err != nil { 52 if err != nil {
48 fmt.Printf("logger: new: couldn't create error log file\n") 53 fmt.Printf("logger: new: couldn't create error log file\n")
49 return &l, err 54 return &l, err
50 } 55 }
51 56
52 // create directories to keep log files 57 // create directories to keep log files
53 err = os.Mkdir(EvtDirName, os.ModePerm) 58 err = os.Mkdir(EvtDirName, os.ModePerm)
54 if err != nil { 59 if err != nil {
55 if !os.IsExist(err) { 60 if !os.IsExist(err) {
56 fmt.Printf("logger: mkdir: couldn't create event log directory\n") 61 fmt.Printf("logger: mkdir: couldn't create event log directory\n")
57 return &l, err 62 return &l, err
58 } 63 }
59 } 64 }
60 65
61 err = os.Mkdir(ErrDirName, os.ModePerm) 66 err = os.Mkdir(ErrDirName, os.ModePerm)
62 if err != nil { 67 if err != nil {
63 if !os.IsExist(err) { 68 if !os.IsExist(err) {
64 fmt.Printf("logger: new: couldn't create error log directory\n") 69 fmt.Printf("logger: new: couldn't create error log directory\n")
65 return &l, err 70 return &l, err
66 } 71 }
67 } 72 }
68 73
69 return &l, nil 74 return &l, nil
70 } 75 }
71 76
72 func (l *Logger) Close() { 77 func (l *Logger) Close() {
73 // close files 78 // close files
74 err := l.eventf.Close() 79 err := l.eventf.Close()
75 if err != nil { 80 if err != nil {
76 fmt.Printf("logger: on exit: couldn't close event log file\n") 81 fmt.Printf("logger: on exit: couldn't close event log file\n")
77 } 82 }
78 83
79 err = l.errorf.Close() 84 err = l.errorf.Close()
80 if err != nil { 85 if err != nil {
81 fmt.Printf("logger: on exit: couldn't close error log file\n") 86 fmt.Printf("logger: on exit: couldn't close error log file\n")
82 } 87 }
83 } 88 }
84 89
85 func (l *Logger) splitEventLog() { 90 func (l *Logger) splitEventLog() {
86 timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10) 91 timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10)
87 92
88 // events 93 muEv.Lock()
94 defer muEv.Unlock()
95
89 evfstats, _ := l.eventf.Stat() 96 evfstats, _ := l.eventf.Stat()
90 if evfstats.Size() >= MaxLogFileSize100KB { 97 if evfstats.Size() >= MaxLogFileSize100KB {
91 // close old file 98 // close old file
92 err := l.eventf.Close() 99 err := l.eventf.Close()
93 if err != nil { 100 if err != nil {
94 fmt.Printf("logger: split: couldn't close event file\n") 101 fmt.Printf("logger: split: couldn't close event file\n")
95 return 102 return
96 } 103 }
97 // open new file 104 // open new file
98 var errnew error 105 var errnew error
99 l.eventf, errnew = os.Create("./" + EvtDirName + "/" + 106 l.eventf, errnew = os.Create("./" + EvtDirName + "/" +
100 l.eventFileName + timestamp + ".txt") 107 l.eventFileName + timestamp + ".txt")
101 108
102 if errnew != nil { 109 if errnew != nil {
103 fmt.Printf("logger: split: couldn't create event log file\n") 110 fmt.Printf("logger: split: couldn't create event log file\n")
104 } 111 }
105 } 112 }
106 } 113 }
107 114
108 func (l *Logger) splitErrorLog() { 115 func (l *Logger) splitErrorLog() {
109 timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10) 116 timestamp := "_" + strconv.FormatInt(time.Now().Unix(), 10)
110 117
118 muEr.Lock()
119 defer muEr.Unlock()
120
111 erfstats, _ := l.errorf.Stat() 121 erfstats, _ := l.errorf.Stat()
112 if erfstats.Size() >= MaxLogFileSize100KB { 122 if erfstats.Size() >= MaxLogFileSize100KB {
113 // close old file 123 // close old file
114 err := l.errorf.Close() 124 err := l.errorf.Close()
115 if err != nil { 125 if err != nil {
116 fmt.Printf("logger: split: couldn't close error file\n") 126 fmt.Printf("logger: split: couldn't close error file\n")
117 return 127 return
118 } 128 }
119 // open new file 129 // open new file
120 var errnew error 130 var errnew error
121 l.errorf, errnew = os.Create("./" + ErrDirName + "/" + 131 l.errorf, errnew = os.Create("./" + ErrDirName + "/" +
122 l.errorFileName + timestamp + ".txt") 132 l.errorFileName + timestamp + ".txt")
123 133
124 if errnew != nil { 134 if errnew != nil {
125 fmt.Printf("logger: split: couldn't create error log file\n") 135 fmt.Printf("logger: split: couldn't create error log file\n")
126 } 136 }
127 } 137 }
128 } 138 }
129 139
130 func (l *Logger) LogEvent(event string) { 140 func (l *Logger) LogEvent(event string) {
141 muEv.Lock()
142 defer muEv.Unlock()
131 l.eventf.WriteString(time.Now().Format(time.UnixDate) + ": " + event + "\n") 143 l.eventf.WriteString(time.Now().Format(time.UnixDate) + ": " + event + "\n")
132 l.splitEventLog() 144 l.splitEventLog()
133 } 145 }
134 146
135 func (l *Logger) LogError(err error) { 147 func (l *Logger) LogError(err error) {
148 muEr.Lock()
149 defer muEr.Unlock()
136 l.errorf.WriteString(time.Now().Format(time.UnixDate) + ": " + err.Error() + "\n") 150 l.errorf.WriteString(time.Now().Format(time.UnixDate) + ": " + err.Error() + "\n")
137 l.splitErrorLog() 151 l.splitErrorLog()
138 } 152 }
139 153