Blame view
main.go
4.52 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 26 27 |
const ErrDirName = "error-logs" const EvtDirName = "event-logs" |
434e7da25 changed initializ... |
28 |
type goLogger struct { |
23ce66f0f optional init par... |
29 30 |
muEv *sync.Mutex eventf *os.File |
a99c98307 first commit |
31 |
eventFileName string |
23ce66f0f optional init par... |
32 33 34 |
muEr *sync.Mutex errorf *os.File |
a99c98307 first commit |
35 |
errorFileName string |
434e7da25 changed initializ... |
36 37 |
splitSize int64 |
a99c98307 first commit |
38 |
} |
434e7da25 changed initializ... |
39 |
var logger goLogger |
a99c98307 first commit |
40 |
|
434e7da25 changed initializ... |
41 |
func Init(flags Option, splitSize int64) error { |
ce403bd83 changed timestamp... |
42 |
timestamp := "_" + time.Now().Format(time.RFC3339) |
23ce66f0f optional init par... |
43 |
var err error |
434e7da25 changed initializ... |
44 |
logger.splitSize = splitSize |
23ce66f0f optional init par... |
45 |
// event file/dir |
434e7da25 changed initializ... |
46 |
if flags&Events > 0 { |
23ce66f0f optional init par... |
47 48 49 50 51 52 53 |
err = os.Mkdir(EvtDirName, os.ModePerm) if err != nil { if !os.IsExist(err) { fmt.Printf("logger: mkdir: couldn't create event log directory ") return err } |
a99c98307 first commit |
54 |
} |
a99c98307 first commit |
55 |
|
434e7da25 changed initializ... |
56 57 |
logger.eventFileName = "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 ") |
98708cdaf new version, does... |
62 |
return 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 { |
23ce66f0f optional init par... |
69 70 71 72 73 74 75 76 |
err = os.Mkdir(ErrDirName, os.ModePerm) if err != nil { if !os.IsExist(err) { fmt.Printf("logger: new: couldn't create error log directory ") return err } } |
e025a8738 platform agnostic... |
77 |
|
434e7da25 changed initializ... |
78 79 |
logger.errorFileName = "errors" path := filepath.Join(ErrDirName, logger.errorFileName+timestamp+".txt") |
23ce66f0f optional init par... |
80 81 82 83 84 85 |
logger.errorf, err = os.Create(path) if err != nil { fmt.Printf("logger: new: couldn't create error log file ") return err } |
e42ead15b mutex initialization |
86 87 |
logger.muEr = &sync.Mutex{} |
e025a8738 platform agnostic... |
88 |
} |
98708cdaf new version, does... |
89 90 |
return nil } |
327c98634 added Print() for... |
91 92 93 94 |
func Print(s string) { fmt.Printf(time.Now().Format(time.RFC3339) + ": " + s + " ") } |
98708cdaf new version, does... |
95 |
func 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 104 105 |
} func LogError(comment string, err error) { |
23ce66f0f optional init par... |
106 107 108 109 110 111 112 |
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... |
113 |
} |
2f4b177f0 trace log and tra... |
114 |
func TraceEvent(event string) { |
23ce66f0f optional init par... |
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
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... |
129 |
} |
98708cdaf new version, does... |
130 |
} |
2f4b177f0 trace log and tra... |
131 |
func TraceError(err error) { |
23ce66f0f optional init par... |
132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
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... |
146 |
} |
a99c98307 first commit |
147 |
} |
98708cdaf new version, does... |
148 |
func Close() { |
23ce66f0f optional init par... |
149 150 151 152 153 154 |
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 |
155 |
} |
23ce66f0f optional init par... |
156 157 158 159 160 161 |
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 |
162 163 |
} } |
434e7da25 changed initializ... |
164 |
func (l *goLogger) splitEventLog() { |
ce403bd83 changed timestamp... |
165 |
timestamp := "_" + time.Now().Format(time.RFC3339) |
a99c98307 first commit |
166 |
|
a99c98307 first commit |
167 |
evfstats, _ := l.eventf.Stat() |
434e7da25 changed initializ... |
168 |
if evfstats.Size() >= l.splitSize { |
a99c98307 first commit |
169 170 171 172 173 174 175 176 177 |
// 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 |
e025a8738 platform agnostic... |
178 179 |
path := filepath.Join(EvtDirName, l.eventFileName+timestamp) l.eventf, errnew = os.Create(path) |
a99c98307 first commit |
180 181 182 183 184 185 186 |
if errnew != nil { fmt.Printf("logger: split: couldn't create event log file ") } } } |
434e7da25 changed initializ... |
187 |
func (l *goLogger) splitErrorLog() { |
ce403bd83 changed timestamp... |
188 |
timestamp := "_" + time.Now().Format(time.RFC3339) |
a99c98307 first commit |
189 190 |
erfstats, _ := l.errorf.Stat() |
434e7da25 changed initializ... |
191 |
if erfstats.Size() >= l.splitSize { |
a99c98307 first commit |
192 193 194 195 196 197 198 199 200 |
// 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 |
e025a8738 platform agnostic... |
201 202 |
path := filepath.Join(ErrDirName, l.errorFileName+timestamp) l.errorf, errnew = os.Create(path) |
a99c98307 first commit |
203 204 205 206 207 208 209 |
if errnew != nil { fmt.Printf("logger: split: couldn't create error log file ") } } } |