Commit ed6549a3365b468fba5865eed219826e8cf9366b
1 parent
372beaf627
Exists in
master
minor output format changes
Showing
1 changed file
with
3 additions
and
3 deletions
Show diff stats
main.go
1 | package gologger | 1 | package gologger |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "net/http" | 5 | "net/http" |
6 | "net/http/httputil" | 6 | "net/http/httputil" |
7 | "os" | 7 | "os" |
8 | "path/filepath" | 8 | "path/filepath" |
9 | "runtime" | 9 | "runtime" |
10 | "strings" | 10 | "strings" |
11 | "sync" | 11 | "sync" |
12 | "time" | 12 | "time" |
13 | ) | 13 | ) |
14 | 14 | ||
15 | const ( | 15 | const ( |
16 | MaxLogSize5MB int64 = 5 * 1024 * 1024 | 16 | MaxLogSize5MB int64 = 5 * 1024 * 1024 |
17 | MaxLogSize1MB int64 = 1 * 1024 * 1024 | 17 | MaxLogSize1MB int64 = 1 * 1024 * 1024 |
18 | MaxLogSize500KB int64 = 500 * 1024 | 18 | MaxLogSize500KB int64 = 500 * 1024 |
19 | MaxLogSize100KB int64 = 100 * 1024 | 19 | MaxLogSize100KB int64 = 100 * 1024 |
20 | MaxLogSize512B int64 = 512 | 20 | MaxLogSize512B int64 = 512 |
21 | logDirName = "log" | 21 | logDirName = "log" |
22 | ) | 22 | ) |
23 | 23 | ||
24 | type Logger struct { | 24 | type Logger struct { |
25 | mu *sync.Mutex | 25 | mu *sync.Mutex |
26 | outputFile *os.File | 26 | outputFile *os.File |
27 | 27 | ||
28 | outputFileName string | 28 | outputFileName string |
29 | maxFileSize int64 | 29 | maxFileSize int64 |
30 | 30 | ||
31 | splitCount int | 31 | splitCount int |
32 | } | 32 | } |
33 | 33 | ||
34 | func New(name string, maxFileSize int64) (logger *Logger, err error) { | 34 | func New(name string, maxFileSize int64) (logger *Logger, err error) { |
35 | logger = &Logger{} | 35 | logger = &Logger{} |
36 | 36 | ||
37 | logger.outputFileName = name + "-log" | 37 | logger.outputFileName = name + "-log" |
38 | logger.mu = &sync.Mutex{} | 38 | logger.mu = &sync.Mutex{} |
39 | logger.maxFileSize = maxFileSize | 39 | logger.maxFileSize = maxFileSize |
40 | 40 | ||
41 | err = os.Mkdir(logDirName, os.ModePerm) | 41 | err = os.Mkdir(logDirName, os.ModePerm) |
42 | if err != nil { | 42 | if err != nil { |
43 | if !os.IsExist(err) { | 43 | if !os.IsExist(err) { |
44 | fmt.Printf("logger: mkdir: couldn't create event log directory\n") | 44 | fmt.Printf("logger: mkdir: couldn't create event log directory\n") |
45 | return nil, err | 45 | return nil, err |
46 | } | 46 | } |
47 | } | 47 | } |
48 | 48 | ||
49 | date := strings.Replace(time.Now().Format(time.RFC3339), ":", ".", -1) | 49 | date := strings.Replace(time.Now().Format(time.RFC3339), ":", ".", -1) |
50 | logger.outputFileName += "_" + date + ".txt" | 50 | logger.outputFileName += "_" + date + ".txt" |
51 | path := filepath.Join(logDirName, logger.outputFileName) | 51 | path := filepath.Join(logDirName, logger.outputFileName) |
52 | logger.outputFile, err = os.Create(path) | 52 | logger.outputFile, err = os.Create(path) |
53 | if err != nil { | 53 | if err != nil { |
54 | fmt.Printf("logger: new: couldn't create event log file\n") | 54 | fmt.Printf("logger: new: couldn't create event log file\n") |
55 | return nil, err | 55 | return nil, err |
56 | } | 56 | } |
57 | 57 | ||
58 | return logger, nil | 58 | return logger, nil |
59 | } | 59 | } |
60 | 60 | ||
61 | func (l *Logger) Print(format string, v ...interface{}) { | 61 | func (l *Logger) Print(format string, v ...interface{}) { |
62 | msg := fmt.Sprintf(format, v...) | 62 | msg := fmt.Sprintf(format, v...) |
63 | fmt.Printf(time.Now().Format(time.RFC3339) + ": " + msg + "\n") | 63 | fmt.Printf(time.Now().Format(time.RFC3339) + ": " + msg + "\n") |
64 | } | 64 | } |
65 | 65 | ||
66 | func (l *Logger) Log(format string, v ...interface{}) { | 66 | func (l *Logger) Log(format string, v ...interface{}) { |
67 | if l.outputFile == nil { | 67 | if l.outputFile == nil { |
68 | return | 68 | return |
69 | } | 69 | } |
70 | 70 | ||
71 | l.mu.Lock() | 71 | l.mu.Lock() |
72 | defer l.mu.Unlock() | 72 | defer l.mu.Unlock() |
73 | 73 | ||
74 | msg := fmt.Sprintf(format, v...) | 74 | msg := fmt.Sprintf(format, v...) |
75 | s := time.Now().Format(time.RFC3339) + ": " + msg + "\n" | 75 | s := time.Now().Format(time.RFC3339) + ": " + msg + "\n" |
76 | if l.shouldSplit(len(s)) { | 76 | if l.shouldSplit(len(s)) { |
77 | l.split() | 77 | l.split() |
78 | } | 78 | } |
79 | l.outputFile.WriteString(s) | 79 | l.outputFile.WriteString(s) |
80 | } | 80 | } |
81 | 81 | ||
82 | func (l *Logger) RequestLog(req *http.Request, userid string) string { | 82 | func (l *Logger) RequestLog(req *http.Request, userid string) string { |
83 | if l.outputFile == nil { | 83 | if l.outputFile == nil { |
84 | return "" | 84 | return "" |
85 | } | 85 | } |
86 | 86 | ||
87 | if userid == "" { | 87 | if userid == "" { |
88 | userid = "-" | 88 | userid = "-" |
89 | } | 89 | } |
90 | 90 | ||
91 | var b strings.Builder | 91 | var b strings.Builder |
92 | b.WriteString("Request:\n") | 92 | b.WriteString("Request:\n") |
93 | // CLF-like header | 93 | // CLF-like header |
94 | ts := time.Now().Format(time.RFC3339) | 94 | ts := time.Now().Format(time.RFC3339) |
95 | fmt.Fprintf(&b, "%s %s\n", req.RemoteAddr, ts) | 95 | fmt.Fprintf(&b, "%s %s\n", req.RemoteAddr, ts) |
96 | 96 | ||
97 | body, err := httputil.DumpRequest(req, true) | 97 | body, err := httputil.DumpRequest(req, true) |
98 | if err != nil { | 98 | if err != nil { |
99 | fmt.Fprintf(os.Stderr, "%v\n", err) | 99 | fmt.Fprintf(os.Stderr, "%v\n", err) |
100 | } else { | 100 | } else { |
101 | b.WriteString(string(body)) | 101 | b.WriteString(string(body)) |
102 | } | 102 | } |
103 | b.WriteString("\n\n") | 103 | b.WriteString("\n\n") |
104 | 104 | ||
105 | msg := b.String() | 105 | msg := b.String() |
106 | 106 | ||
107 | return msg | 107 | return msg |
108 | } | 108 | } |
109 | 109 | ||
110 | func (l *Logger) ResponseLog(status int, duration time.Duration) string { | 110 | func (l *Logger) ResponseLog(status int, duration time.Duration, size int64) string { |
111 | if l.outputFile == nil { | 111 | if l.outputFile == nil { |
112 | return "" | 112 | return "" |
113 | } | 113 | } |
114 | 114 | ||
115 | var b strings.Builder | 115 | var b strings.Builder |
116 | fmt.Fprintf(&b, "Response: %d %v\n", status, duration) | 116 | fmt.Fprintf(&b, "Response:\n%d %v %dB\n", status, duration, size) |
117 | b.WriteString("==============================================================\n\n") | 117 | b.WriteString("==============================================================\n") |
118 | msg := b.String() | 118 | msg := b.String() |
119 | 119 | ||
120 | return msg | 120 | return msg |
121 | } | 121 | } |
122 | 122 | ||
123 | func (l *Logger) LogHTTPTraffic(in string, out string) { | 123 | func (l *Logger) LogHTTPTraffic(in string, out string) { |
124 | if l.outputFile == nil { | 124 | if l.outputFile == nil { |
125 | return | 125 | return |
126 | } | 126 | } |
127 | 127 | ||
128 | msg := in + out | 128 | msg := in + out |
129 | 129 | ||
130 | l.mu.Lock() | 130 | l.mu.Lock() |
131 | defer l.mu.Unlock() | 131 | defer l.mu.Unlock() |
132 | 132 | ||
133 | if l.shouldSplit(len(msg)) { | 133 | if l.shouldSplit(len(msg)) { |
134 | l.split() | 134 | l.split() |
135 | } | 135 | } |
136 | l.outputFile.WriteString(msg) | 136 | l.outputFile.WriteString(msg) |
137 | } | 137 | } |
138 | 138 | ||
139 | func (l *Logger) Trace(format string, v ...interface{}) { | 139 | func (l *Logger) Trace(format string, v ...interface{}) { |
140 | if l.outputFile == nil { | 140 | if l.outputFile == nil { |
141 | return | 141 | return |
142 | } | 142 | } |
143 | 143 | ||
144 | l.mu.Lock() | 144 | l.mu.Lock() |
145 | defer l.mu.Unlock() | 145 | defer l.mu.Unlock() |
146 | _, file, line, ok := runtime.Caller(1) | 146 | _, file, line, ok := runtime.Caller(1) |
147 | 147 | ||
148 | s := "" | 148 | s := "" |
149 | msg := fmt.Sprintf(format, v...) | 149 | msg := fmt.Sprintf(format, v...) |
150 | if ok { | 150 | if ok { |
151 | s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg) | 151 | s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg) |
152 | } else { | 152 | } else { |
153 | s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + msg + "\n") | 153 | s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + msg + "\n") |
154 | } | 154 | } |
155 | if l.shouldSplit(len(s)) { | 155 | if l.shouldSplit(len(s)) { |
156 | l.split() | 156 | l.split() |
157 | } | 157 | } |
158 | l.outputFile.WriteString(s) | 158 | l.outputFile.WriteString(s) |
159 | } | 159 | } |
160 | 160 | ||
161 | func (l *Logger) Close() { | 161 | func (l *Logger) Close() { |
162 | if l.outputFile == nil { | 162 | if l.outputFile == nil { |
163 | return | 163 | return |
164 | } | 164 | } |
165 | 165 | ||
166 | err := l.outputFile.Close() | 166 | err := l.outputFile.Close() |
167 | if err != nil { | 167 | if err != nil { |
168 | fmt.Printf("logger: on exit: couldn't close event log file\n") | 168 | fmt.Printf("logger: on exit: couldn't close event log file\n") |
169 | } | 169 | } |
170 | } | 170 | } |
171 | 171 | ||
172 | func (l *Logger) split() { | 172 | func (l *Logger) split() { |
173 | if l.outputFile == nil { | 173 | if l.outputFile == nil { |
174 | return | 174 | return |
175 | } | 175 | } |
176 | 176 | ||
177 | // close old file | 177 | // close old file |
178 | err := l.outputFile.Close() | 178 | err := l.outputFile.Close() |
179 | if err != nil { | 179 | if err != nil { |
180 | fmt.Printf("logger: split: couldn't close event file\n") | 180 | fmt.Printf("logger: split: couldn't close event file\n") |
181 | return | 181 | return |
182 | } | 182 | } |
183 | 183 | ||
184 | l.splitCount++ | 184 | l.splitCount++ |
185 | // open new file | 185 | // open new file |
186 | var errnew error | 186 | var errnew error |
187 | path := filepath.Join(logDirName, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)) | 187 | path := filepath.Join(logDirName, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)) |
188 | l.outputFile, errnew = os.Create(path) | 188 | l.outputFile, errnew = os.Create(path) |
189 | 189 | ||
190 | if errnew != nil { | 190 | if errnew != nil { |
191 | fmt.Printf("logger: split: couldn't create event log file\n") | 191 | fmt.Printf("logger: split: couldn't create event log file\n") |
192 | } | 192 | } |
193 | } | 193 | } |
194 | 194 | ||
195 | func (l *Logger) shouldSplit(nextEntrySize int) bool { | 195 | func (l *Logger) shouldSplit(nextEntrySize int) bool { |
196 | stats, _ := l.outputFile.Stat() | 196 | stats, _ := l.outputFile.Stat() |
197 | return int64(nextEntrySize) >= (l.maxFileSize - stats.Size()) | 197 | return int64(nextEntrySize) >= (l.maxFileSize - stats.Size()) |
198 | } | 198 | } |
199 | 199 |