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