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