Commit 5eaa49d2a41944dece41e209811d56e2959c0371
1 parent
e42ead15ba
Exists in
master
new design; allow for multiple instances
Showing
1 changed file
with
28 additions
and
29 deletions
Show diff stats
main.go
1 | package gologger | 1 | package gologger |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "os" | 5 | "os" |
6 | "path/filepath" | 6 | "path/filepath" |
7 | "runtime" | 7 | "runtime" |
8 | "sync" | 8 | "sync" |
9 | "time" | 9 | "time" |
10 | ) | 10 | ) |
11 | 11 | ||
12 | type Option uint8 | 12 | type Option uint8 |
13 | 13 | ||
14 | const ( | 14 | const ( |
15 | Events Option = 0x01 | 15 | Events Option = 0x01 |
16 | Errors Option = 0x02 | 16 | Errors Option = 0x02 |
17 | ) | 17 | ) |
18 | 18 | ||
19 | const ( | 19 | const ( |
20 | MaxLogSize5MB int64 = 5 * 1024 * 1024 | 20 | MaxLogSize5MB int64 = 5 * 1024 * 1024 |
21 | MaxLogSize1MB int64 = 1 * 1024 * 1024 | 21 | MaxLogSize1MB int64 = 1 * 1024 * 1024 |
22 | MaxLogSize500KB int64 = 500 * 1024 | 22 | MaxLogSize500KB int64 = 500 * 1024 |
23 | MaxLogSize100KB int64 = 100 * 1024 | 23 | MaxLogSize100KB int64 = 100 * 1024 |
24 | MaxLogSize512B int64 = 512 | 24 | MaxLogSize512B int64 = 512 |
25 | ) | 25 | ) |
26 | 26 | ||
27 | const ErrDirName = "error-logs" | 27 | const errDirName = "error-logs" |
28 | const EvtDirName = "event-logs" | 28 | const evtDirName = "event-logs" |
29 | 29 | ||
30 | type goLogger struct { | 30 | type Logger struct { |
31 | muEv *sync.Mutex | 31 | muEv *sync.Mutex |
32 | eventf *os.File | 32 | eventf *os.File |
33 | 33 | ||
34 | eventFileName string | 34 | eventFileName string |
35 | 35 | ||
36 | muEr *sync.Mutex | 36 | muEr *sync.Mutex |
37 | errorf *os.File | 37 | errorf *os.File |
38 | 38 | ||
39 | errorFileName string | 39 | errorFileName string |
40 | 40 | ||
41 | splitSize int64 | 41 | splitSize int64 |
42 | } | 42 | } |
43 | 43 | ||
44 | var logger goLogger | 44 | func New(name string, flags Option, splitSize int64) (logger *Logger, err error) { |
45 | 45 | logger = &Logger{} | |
46 | func Init(flags Option, splitSize int64) error { | ||
47 | timestamp := "_" + time.Now().Format(time.RFC3339) | ||
48 | var err error | ||
49 | 46 | ||
50 | logger.splitSize = splitSize | 47 | logger.splitSize = splitSize |
51 | 48 | ||
49 | timestamp := "_" + time.Now().Format(time.RFC3339) | ||
50 | |||
52 | // event file/dir | 51 | // event file/dir |
53 | if flags&Events > 0 { | 52 | if flags&Events > 0 { |
54 | err = os.Mkdir(EvtDirName, os.ModePerm) | 53 | err = os.Mkdir(evtDirName, os.ModePerm) |
55 | if err != nil { | 54 | if err != nil { |
56 | if !os.IsExist(err) { | 55 | if !os.IsExist(err) { |
57 | fmt.Printf("logger: mkdir: couldn't create event log directory\n") | 56 | fmt.Printf("logger: mkdir: couldn't create event log directory\n") |
58 | return err | 57 | return nil, err |
59 | } | 58 | } |
60 | } | 59 | } |
61 | 60 | ||
62 | logger.eventFileName = "events" | 61 | logger.eventFileName = name + "-events" |
63 | path := filepath.Join(EvtDirName, logger.eventFileName+timestamp+".txt") | 62 | path := filepath.Join(evtDirName, logger.eventFileName+timestamp+".txt") |
64 | logger.eventf, err = os.Create(path) | 63 | logger.eventf, err = os.Create(path) |
65 | if err != nil { | 64 | if err != nil { |
66 | fmt.Printf("logger: new: couldn't create event log file\n") | 65 | fmt.Printf("logger: new: couldn't create event log file\n") |
67 | return err | 66 | return nil, err |
68 | } | 67 | } |
69 | 68 | ||
70 | logger.muEv = &sync.Mutex{} | 69 | logger.muEv = &sync.Mutex{} |
71 | } | 70 | } |
72 | 71 | ||
73 | // error file/dir | 72 | // error file/dir |
74 | if flags&Errors > 0 { | 73 | if flags&Errors > 0 { |
75 | err = os.Mkdir(ErrDirName, os.ModePerm) | 74 | err = os.Mkdir(errDirName, os.ModePerm) |
76 | if err != nil { | 75 | if err != nil { |
77 | if !os.IsExist(err) { | 76 | if !os.IsExist(err) { |
78 | fmt.Printf("logger: new: couldn't create error log directory\n") | 77 | fmt.Printf("logger: new: couldn't create error log directory\n") |
79 | return err | 78 | return nil, err |
80 | } | 79 | } |
81 | } | 80 | } |
82 | 81 | ||
83 | logger.errorFileName = "errors" | 82 | logger.errorFileName = name + "-errors" |
84 | path := filepath.Join(ErrDirName, logger.errorFileName+timestamp+".txt") | 83 | path := filepath.Join(errDirName, logger.errorFileName+timestamp+".txt") |
85 | logger.errorf, err = os.Create(path) | 84 | logger.errorf, err = os.Create(path) |
86 | if err != nil { | 85 | if err != nil { |
87 | fmt.Printf("logger: new: couldn't create error log file\n") | 86 | fmt.Printf("logger: new: couldn't create error log file\n") |
88 | return err | 87 | return nil, err |
89 | } | 88 | } |
90 | 89 | ||
91 | logger.muEr = &sync.Mutex{} | 90 | logger.muEr = &sync.Mutex{} |
92 | } | 91 | } |
93 | 92 | ||
94 | return nil | 93 | return logger, nil |
95 | } | 94 | } |
96 | 95 | ||
97 | func Print(s string) { | 96 | func (logger *Logger) Print(s string) { |
98 | fmt.Printf(time.Now().Format(time.RFC3339) + ": " + s + "\n") | 97 | fmt.Printf(time.Now().Format(time.RFC3339) + ": " + s + "\n") |
99 | } | 98 | } |
100 | 99 | ||
101 | func LogEvent(event string) { | 100 | func (logger *Logger) LogEvent(event string) { |
102 | if logger.eventf != nil { | 101 | if logger.eventf != nil { |
103 | logger.muEv.Lock() | 102 | logger.muEv.Lock() |
104 | defer logger.muEv.Unlock() | 103 | defer logger.muEv.Unlock() |
105 | logger.eventf.WriteString(time.Now().Format(time.RFC3339) + ": " + event + "\n") | 104 | logger.eventf.WriteString(time.Now().Format(time.RFC3339) + ": " + event + "\n") |
106 | logger.splitEventLog() | 105 | logger.splitEventLog() |
107 | } | 106 | } |
108 | } | 107 | } |
109 | 108 | ||
110 | func LogError(comment string, err error) { | 109 | func (logger *Logger) LogError(comment string, err error) { |
111 | if logger.errorf != nil { | 110 | if logger.errorf != nil { |
112 | logger.muEr.Lock() | 111 | logger.muEr.Lock() |
113 | defer logger.muEr.Unlock() | 112 | defer logger.muEr.Unlock() |
114 | logger.errorf.WriteString(time.Now().Format(time.RFC3339) + ": " + comment + ": " + err.Error() + "\n") | 113 | logger.errorf.WriteString(time.Now().Format(time.RFC3339) + ": " + comment + ": " + err.Error() + "\n") |
115 | logger.splitErrorLog() | 114 | logger.splitErrorLog() |
116 | } | 115 | } |
117 | } | 116 | } |
118 | 117 | ||
119 | func TraceEvent(event string) { | 118 | func (logger *Logger) TraceEvent(event string) { |
120 | if logger.eventf != nil { | 119 | if logger.eventf != nil { |
121 | logger.muEv.Lock() | 120 | logger.muEv.Lock() |
122 | defer logger.muEv.Unlock() | 121 | defer logger.muEv.Unlock() |
123 | _, file, line, ok := runtime.Caller(1) | 122 | _, file, line, ok := runtime.Caller(1) |
124 | var s string | 123 | var s string |
125 | if ok { | 124 | if ok { |
126 | s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, event) | 125 | s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, event) |
127 | } else { | 126 | } else { |
128 | s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + event + "\n") | 127 | s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + event + "\n") |
129 | } | 128 | } |
130 | logger.eventf.WriteString(s) | 129 | logger.eventf.WriteString(s) |
131 | logger.splitEventLog() | 130 | logger.splitEventLog() |
132 | } | 131 | } |
133 | } | 132 | } |
134 | 133 | ||
135 | func TraceError(err error) { | 134 | func (logger *Logger) TraceError(err error) { |
136 | if logger.errorf != nil { | 135 | if logger.errorf != nil { |
137 | logger.muEr.Lock() | 136 | logger.muEr.Lock() |
138 | defer logger.muEr.Unlock() | 137 | defer logger.muEr.Unlock() |
139 | _, file, line, ok := runtime.Caller(1) | 138 | _, file, line, ok := runtime.Caller(1) |
140 | var s string | 139 | var s string |
141 | if ok { | 140 | if ok { |
142 | s = fmt.Sprintf("%s %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, err.Error()) | 141 | s = fmt.Sprintf("%s %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, err.Error()) |
143 | } else { | 142 | } else { |
144 | s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + err.Error() + "\n") | 143 | s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + err.Error() + "\n") |
145 | } | 144 | } |
146 | logger.errorf.WriteString(s) | 145 | logger.errorf.WriteString(s) |
147 | logger.splitErrorLog() | 146 | logger.splitErrorLog() |
148 | } | 147 | } |
149 | } | 148 | } |
150 | 149 | ||
151 | func Close() { | 150 | func (logger *Logger) Close() { |
152 | if logger.eventf != nil { | 151 | if logger.eventf != nil { |
153 | err := logger.eventf.Close() | 152 | err := logger.eventf.Close() |
154 | if err != nil { | 153 | if err != nil { |
155 | fmt.Printf("logger: on exit: couldn't close event log file\n") | 154 | fmt.Printf("logger: on exit: couldn't close event log file\n") |
156 | } | 155 | } |
157 | } | 156 | } |
158 | 157 | ||
159 | if logger.errorf != nil { | 158 | if logger.errorf != nil { |
160 | err := logger.errorf.Close() | 159 | err := logger.errorf.Close() |
161 | if err != nil { | 160 | if err != nil { |
162 | fmt.Printf("logger: on exit: couldn't close error log file\n") | 161 | fmt.Printf("logger: on exit: couldn't close error log file\n") |
163 | } | 162 | } |
164 | } | 163 | } |
165 | } | 164 | } |
166 | 165 | ||
167 | func (l *goLogger) splitEventLog() { | 166 | func (l *Logger) splitEventLog() { |
168 | timestamp := "_" + time.Now().Format(time.RFC3339) | 167 | timestamp := "_" + time.Now().Format(time.RFC3339) |
169 | 168 | ||
170 | evfstats, _ := l.eventf.Stat() | 169 | evfstats, _ := l.eventf.Stat() |
171 | if evfstats.Size() >= l.splitSize { | 170 | if evfstats.Size() >= l.splitSize { |
172 | // close old file | 171 | // close old file |
173 | err := l.eventf.Close() | 172 | err := l.eventf.Close() |
174 | if err != nil { | 173 | if err != nil { |
175 | fmt.Printf("logger: split: couldn't close event file\n") | 174 | fmt.Printf("logger: split: couldn't close event file\n") |
176 | return | 175 | return |
177 | } | 176 | } |
178 | // open new file | 177 | // open new file |
179 | var errnew error | 178 | var errnew error |
180 | path := filepath.Join(EvtDirName, l.eventFileName+timestamp) | 179 | path := filepath.Join(evtDirName, l.eventFileName+timestamp) |
181 | l.eventf, errnew = os.Create(path) | 180 | l.eventf, errnew = os.Create(path) |
182 | 181 | ||
183 | if errnew != nil { | 182 | if errnew != nil { |
184 | fmt.Printf("logger: split: couldn't create event log file\n") | 183 | fmt.Printf("logger: split: couldn't create event log file\n") |
185 | } | 184 | } |
186 | } | 185 | } |
187 | } | 186 | } |
188 | 187 | ||
189 | func (l *goLogger) splitErrorLog() { | 188 | func (l *Logger) splitErrorLog() { |
190 | timestamp := "_" + time.Now().Format(time.RFC3339) | 189 | timestamp := "_" + time.Now().Format(time.RFC3339) |
191 | 190 | ||
192 | erfstats, _ := l.errorf.Stat() | 191 | erfstats, _ := l.errorf.Stat() |
193 | if erfstats.Size() >= l.splitSize { | 192 | if erfstats.Size() >= l.splitSize { |
194 | // close old file | 193 | // close old file |
195 | err := l.errorf.Close() | 194 | err := l.errorf.Close() |
196 | if err != nil { | 195 | if err != nil { |
197 | fmt.Printf("logger: split: couldn't close error file\n") | 196 | fmt.Printf("logger: split: couldn't close error file\n") |
198 | return | 197 | return |
199 | } | 198 | } |
200 | // open new file | 199 | // open new file |
201 | var errnew error | 200 | var errnew error |
202 | path := filepath.Join(ErrDirName, l.errorFileName+timestamp) | 201 | path := filepath.Join(errDirName, l.errorFileName+timestamp) |
203 | l.errorf, errnew = os.Create(path) | 202 | l.errorf, errnew = os.Create(path) |
204 | 203 | ||
205 | if errnew != nil { | 204 | if errnew != nil { |
206 | fmt.Printf("logger: split: couldn't create error log file\n") | 205 | fmt.Printf("logger: split: couldn't create error log file\n") |
207 | } | 206 | } |
208 | } | 207 | } |