Commit 7fa5b8af23a00f41a1fed32bb58ead498dcc423d

Authored by Marko Tikvić
1 parent ce403bd83b
Exists in master

Changed order of parameters in LogError(). It seems more intuitive to give simple explanation first.

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