Commit 093191eb2230cc24d6d609f6307b8681d9baae3a
1 parent
1f7fbc601b
Exists in
master
much cleaner, much better
Showing
1 changed file
with
14 additions
and
44 deletions
Show diff stats
main.go
... | ... | @@ -81,70 +81,40 @@ func (l *Logger) Log(format string, v ...interface{}) { |
81 | 81 | l.outputFile.WriteString(s) |
82 | 82 | } |
83 | 83 | |
84 | -func (l *Logger) RequestLog(req *http.Request, userID string) string { | |
85 | - if l.outputFile == nil { | |
86 | - return "" | |
87 | - } | |
88 | - | |
84 | +func (l *Logger) LogHTTPRequest(req *http.Request, userID string) string { | |
89 | 85 | if userID == "" { |
90 | 86 | userID = "-" |
91 | 87 | } |
92 | 88 | |
93 | 89 | var b strings.Builder |
90 | + | |
94 | 91 | b.WriteString("Request:\n") |
95 | 92 | // CLF-like header |
96 | - ts := time.Now().Format(time.RFC3339) | |
97 | - fmt.Fprintf(&b, "%s %s %s\n", req.RemoteAddr, userID, ts) | |
98 | - | |
99 | - headers := req.Header | |
100 | - content := headers.Get("Content-Type") | |
101 | - if content != "application/json" { | |
102 | - b.WriteString(fmt.Sprintf("%s %s\n", req.Method, req.URL)) | |
103 | - for k, h := range headers { | |
104 | - b.WriteString(k + ": ") | |
105 | - for _, h2 := range h { | |
106 | - b.WriteString(h2 + " ") | |
107 | - } | |
108 | - b.WriteString("\n") | |
109 | - } | |
110 | - } else { | |
111 | - body, err := httputil.DumpRequest(req, true) | |
112 | - if err != nil { | |
113 | - fmt.Fprintf(os.Stderr, "%v\n", err) | |
114 | - } else { | |
115 | - b.WriteString(string(body)) | |
116 | - } | |
117 | - } | |
118 | - b.WriteString("\n\n") | |
119 | - | |
120 | - msg := b.String() | |
93 | + fmt.Fprintf(&b, "%s %s %s\n", req.RemoteAddr, userID, time.Now().Format(time.RFC3339)) | |
121 | 94 | |
122 | - return msg | |
123 | -} | |
124 | - | |
125 | -func (l *Logger) ResponseLog(status int, duration time.Duration, size int64) string { | |
126 | - if l.outputFile == nil { | |
127 | - return "" | |
95 | + body, err := httputil.DumpRequest(req, true) | |
96 | + if err != nil { | |
97 | + fmt.Fprintf(os.Stderr, "%v\n", err) | |
128 | 98 | } |
99 | + b.WriteString(string(body) + "\n\n") | |
129 | 100 | |
130 | - var b strings.Builder | |
131 | - fmt.Fprintf(&b, "Response:\n%d %v %dB\n", status, duration, size) | |
132 | - b.WriteString("==============================================================\n") | |
133 | - msg := b.String() | |
101 | + return b.String() | |
102 | +} | |
134 | 103 | |
135 | - return msg | |
104 | +func (l *Logger) LogHTTPResponse(status int, duration time.Duration, size int) string { | |
105 | + splitLine := "==============================================================\n" | |
106 | + return fmt.Sprintf("Response:\n%d %v %dB\n%s", status, duration, size, splitLine) | |
136 | 107 | } |
137 | 108 | |
138 | -func (l *Logger) LogHTTPTraffic(in string, out string) { | |
109 | +func (l *Logger) CombineHTTPLogs(in string, out string) { | |
139 | 110 | if l.outputFile == nil { |
140 | 111 | return |
141 | 112 | } |
142 | 113 | |
143 | - msg := in + out | |
144 | - | |
145 | 114 | l.mu.Lock() |
146 | 115 | defer l.mu.Unlock() |
147 | 116 | |
117 | + msg := in + out | |
148 | 118 | if l.shouldSplit(len(msg)) { |
149 | 119 | l.split() |
150 | 120 | } | ... | ... |