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