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