Commit 132f8f2cba2cdd51cd6b3e7c0d8cf9c29c7b5b07

Authored by Marko Tikvić
1 parent baa4468b76
Exists in master

Logger can now print filename and line number

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