Commit 372beaf62734908b8eec30fdc1dd7f76774ffee6
1 parent
dda53db017
Exists in
master
refactored if statements
Showing
1 changed file
with
86 additions
and
73 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 | l.mu.Lock() | 68 | return |
69 | defer l.mu.Unlock() | 69 | } |
70 | 70 | ||
71 | msg := fmt.Sprintf(format, v...) | 71 | l.mu.Lock() |
72 | s := time.Now().Format(time.RFC3339) + ": " + msg + "\n" | 72 | defer l.mu.Unlock() |
73 | if l.shouldSplit(len(s)) { | 73 | |
74 | l.split() | 74 | msg := fmt.Sprintf(format, v...) |
75 | } | 75 | s := time.Now().Format(time.RFC3339) + ": " + msg + "\n" |
76 | l.outputFile.WriteString(s) | 76 | if l.shouldSplit(len(s)) { |
77 | l.split() | ||
77 | } | 78 | } |
79 | l.outputFile.WriteString(s) | ||
78 | } | 80 | } |
79 | 81 | ||
80 | func (l *Logger) LogRequest(req *http.Request, userid string) { | 82 | func (l *Logger) RequestLog(req *http.Request, userid string) string { |
81 | if l.outputFile != nil { | 83 | if l.outputFile == nil { |
82 | if userid == "" { | 84 | return "" |
83 | userid = "-" | 85 | } |
84 | } | ||
85 | 86 | ||
86 | var b strings.Builder | 87 | if userid == "" { |
87 | b.WriteString("Request:\n") | 88 | userid = "-" |
88 | // CLF-like header | 89 | } |
89 | ts := time.Now().Format(time.RFC3339) | ||
90 | fmt.Fprintf(&b, "%s %s\n", req.RemoteAddr, ts) | ||
91 | |||
92 | body, err := httputil.DumpRequest(req, true) | ||
93 | if err != nil { | ||
94 | fmt.Fprintf(os.Stderr, "%v\n", err) | ||
95 | } else { | ||
96 | b.WriteString(string(body)) | ||
97 | } | ||
98 | b.WriteString("\n") | ||
99 | 90 | ||
100 | msg := b.String() | 91 | var b strings.Builder |
92 | b.WriteString("Request:\n") | ||
93 | // CLF-like header | ||
94 | ts := time.Now().Format(time.RFC3339) | ||
95 | fmt.Fprintf(&b, "%s %s\n", req.RemoteAddr, ts) | ||
101 | 96 | ||
102 | l.mu.Lock() | 97 | body, err := httputil.DumpRequest(req, true) |
103 | defer l.mu.Unlock() | 98 | if err != nil { |
99 | fmt.Fprintf(os.Stderr, "%v\n", err) | ||
100 | } else { | ||
101 | b.WriteString(string(body)) | ||
102 | } | ||
103 | b.WriteString("\n\n") | ||
104 | 104 | ||
105 | if l.shouldSplit(len(msg)) { | 105 | msg := b.String() |
106 | l.split() | 106 | |
107 | } | 107 | return msg |
108 | l.outputFile.WriteString(msg) | 108 | } |
109 | |||
110 | func (l *Logger) ResponseLog(status int, duration time.Duration) string { | ||
111 | if l.outputFile == nil { | ||
112 | return "" | ||
109 | } | 113 | } |
114 | |||
115 | var b strings.Builder | ||
116 | fmt.Fprintf(&b, "Response: %d %v\n", status, duration) | ||
117 | b.WriteString("==============================================================\n\n") | ||
118 | msg := b.String() | ||
119 | |||
120 | return msg | ||
110 | } | 121 | } |
111 | 122 | ||
112 | func (l *Logger) LogResponse(w http.ResponseWriter, req *http.Request, duration time.Duration) { | 123 | func (l *Logger) LogHTTPTraffic(in string, out string) { |
113 | if l.outputFile != nil { | 124 | if l.outputFile == nil { |
114 | var b strings.Builder | 125 | return |
115 | b.WriteString("Response:\n") | 126 | } |
116 | fmt.Fprintf(&b, "%s %s\n", req.Method, req.RequestURI) | ||
117 | for k, v := range w.Header() { | ||
118 | fmt.Fprintf(&b, "%s: %s\n", k, v) | ||
119 | } | ||
120 | fmt.Fprintf(&b, "\nCompleted in: %v\n", duration) | ||
121 | b.WriteString("==============================================================\n\n") | ||
122 | msg := b.String() | ||
123 | 127 | ||
124 | l.mu.Lock() | 128 | msg := in + out |
125 | defer l.mu.Unlock() | ||
126 | 129 | ||
127 | if l.shouldSplit(len(msg)) { | 130 | l.mu.Lock() |
128 | l.split() | 131 | defer l.mu.Unlock() |
129 | } | 132 | |
130 | l.outputFile.WriteString(msg) | 133 | if l.shouldSplit(len(msg)) { |
134 | l.split() | ||
131 | } | 135 | } |
136 | l.outputFile.WriteString(msg) | ||
132 | } | 137 | } |
133 | 138 | ||
134 | func (l *Logger) Trace(format string, v ...interface{}) { | 139 | func (l *Logger) Trace(format string, v ...interface{}) { |
135 | if l.outputFile != nil { | 140 | if l.outputFile == nil { |
136 | l.mu.Lock() | 141 | return |
137 | defer l.mu.Unlock() | 142 | } |
138 | _, file, line, ok := runtime.Caller(1) | 143 | |
139 | 144 | l.mu.Lock() | |
140 | s := "" | 145 | defer l.mu.Unlock() |
141 | msg := fmt.Sprintf(format, v...) | 146 | _, file, line, ok := runtime.Caller(1) |
142 | if ok { | 147 | |
143 | s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg) | 148 | s := "" |
144 | } else { | 149 | msg := fmt.Sprintf(format, v...) |
145 | s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + msg + "\n") | 150 | if ok { |
146 | } | 151 | s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg) |
147 | if l.shouldSplit(len(s)) { | 152 | } else { |
148 | l.split() | 153 | s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + msg + "\n") |
149 | } | ||
150 | l.outputFile.WriteString(s) | ||
151 | } | 154 | } |
155 | if l.shouldSplit(len(s)) { | ||
156 | l.split() | ||
157 | } | ||
158 | l.outputFile.WriteString(s) | ||
152 | } | 159 | } |
153 | 160 | ||
154 | func (l *Logger) Close() { | 161 | func (l *Logger) Close() { |
155 | if l.outputFile != nil { | 162 | if l.outputFile == nil { |
156 | err := l.outputFile.Close() | 163 | return |
157 | if err != nil { | 164 | } |
158 | fmt.Printf("logger: on exit: couldn't close event log file\n") | 165 | |
159 | } | 166 | err := l.outputFile.Close() |
167 | if err != nil { | ||
168 | fmt.Printf("logger: on exit: couldn't close event log file\n") | ||
160 | } | 169 | } |
161 | } | 170 | } |
162 | 171 | ||
163 | func (l *Logger) split() { | 172 | func (l *Logger) split() { |
173 | if l.outputFile == nil { | ||
174 | return | ||
175 | } | ||
176 | |||
164 | // close old file | 177 | // close old file |
165 | err := l.outputFile.Close() | 178 | err := l.outputFile.Close() |