Skip to content

Commit ecb698c

Browse files
committed
refactor(http): improve http logging and request tracing
Remove standalone access log middleware and consolidate into context logger. Move logger initialization inside request handler to avoid state reuse. Simplify trace config parsing with early returns. Detect request scheme dynamically instead of defaulting to https.
1 parent f8ebb09 commit ecb698c

4 files changed

Lines changed: 56 additions & 79 deletions

File tree

http/middlewares/accesslog.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

http/middlewares/logger.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,21 @@ func getLogger(req *http.Request) logger.Logger {
6666

6767
func newContextLogger(config TraceConfig) Middleware {
6868
return func(rt http.RoundTripper) http.RoundTripper {
69-
l := &httpretty.Logger{
70-
TLS: config.TLS,
71-
RequestHeader: config.Headers,
72-
RequestBody: config.Body,
73-
ResponseHeader: config.ResponseHeaders,
74-
ResponseBody: config.Response,
75-
Auth: config.Auth,
76-
Colors: true,
77-
Formatters: []httpretty.Formatter{&jsonFormatter{}, &formURLEncodedFormatter{}},
78-
}
79-
l.SkipHeader(logger.SensitiveHeaders)
80-
81-
var buf bytes.Buffer
82-
l.SetOutput(&buf)
83-
inner := l.RoundTripper(rt)
84-
8569
return RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
86-
buf.Reset()
70+
var buf bytes.Buffer
71+
l := &httpretty.Logger{
72+
TLS: config.TLS,
73+
RequestHeader: config.Headers,
74+
RequestBody: config.Body,
75+
ResponseHeader: config.ResponseHeaders,
76+
ResponseBody: config.Response,
77+
Auth: config.Auth,
78+
Colors: true,
79+
Formatters: []httpretty.Formatter{&jsonFormatter{}, &formURLEncodedFormatter{}},
80+
}
81+
l.SkipHeader(logger.SensitiveHeaders)
82+
l.SetOutput(&buf)
83+
inner := l.RoundTripper(rt)
8784
start := time.Now()
8885
resp, err := inner.RoundTrip(req)
8986
elapsed := time.Since(start)
@@ -131,10 +128,10 @@ func newContextAccessLog() Middleware {
131128
elapsed := time.Since(start)
132129
log := getLogger(req)
133130
if err != nil {
134-
log.Infof("%s %s %s %s", console.Bluef(req.Method), console.Yellowf("%s", req.URL), console.Redf("error"), elapsed.Truncate(time.Millisecond))
131+
log.Infof("%s %s %s %s", console.Bluef("%s", req.Method), console.Yellowf("%s", req.URL), console.Redf("error"), elapsed.Truncate(time.Millisecond))
135132
return nil, err
136133
}
137-
log.Infof("%s %s %s %s", console.Bluef(req.Method), console.Yellowf("%s", req.URL), statusColor(resp.StatusCode)("%d", resp.StatusCode), elapsed.Truncate(time.Millisecond))
134+
log.Infof("%s %s %s %s", console.Bluef("%s", req.Method), console.Yellowf("%s", req.URL), statusColor(resp.StatusCode)("%d", resp.StatusCode), elapsed.Truncate(time.Millisecond))
138135
return resp, nil
139136
})
140137
}

http/middlewares/trace.go

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -86,49 +86,48 @@ var traceHeaders = TraceConfig{
8686
}
8787

8888
func TraceConfigFromString(s string) TraceConfig {
89+
var config TraceConfig
8990
switch s {
9091
case "access":
91-
return TraceConfig{AccessLog: true}
92+
config = TraceConfig{AccessLog: true}
9293
case "debug", "headers":
93-
return traceHeaders
94+
config = traceHeaders
9495
case "body":
95-
config := traceHeaders
96+
config = traceHeaders
9697
config.Body = true
97-
return config
9898
case "trace", "all", "response":
99-
return traceAll
100-
}
101-
102-
config := TraceConfig{}
103-
if strings.Contains(s, "all") {
10499
config = traceAll
105-
}
106-
if strings.Contains(s, "headers") {
107-
config.Headers = true
108-
}
109-
if strings.Contains(s, "body") {
110-
config.Body = true
111-
}
112-
if strings.Contains(s, "response") {
113-
config.Response = true
114-
}
115-
if strings.Contains(s, "responseHeaders") {
116-
config.ResponseHeaders = true
117-
}
118-
if strings.Contains(s, "queryParam") {
119-
config.QueryParam = true
120-
}
121-
if strings.Contains(s, "tls") {
122-
config.TLS = true
123-
}
124-
if strings.Contains(s, "timing") {
125-
config.Timing = true
126-
}
127-
if strings.Contains(s, "auth") {
128-
config.Auth = true
129-
}
130-
if strings.Contains(s, "access") {
131-
config.AccessLog = true
100+
default:
101+
if strings.Contains(s, "all") {
102+
config = traceAll
103+
}
104+
if strings.Contains(s, "headers") {
105+
config.Headers = true
106+
}
107+
if strings.Contains(s, "body") {
108+
config.Body = true
109+
}
110+
if strings.Contains(s, "response") {
111+
config.Response = true
112+
}
113+
if strings.Contains(s, "responseHeaders") {
114+
config.ResponseHeaders = true
115+
}
116+
if strings.Contains(s, "queryParam") {
117+
config.QueryParam = true
118+
}
119+
if strings.Contains(s, "tls") {
120+
config.TLS = true
121+
}
122+
if strings.Contains(s, "timing") {
123+
config.Timing = true
124+
}
125+
if strings.Contains(s, "auth") {
126+
config.Auth = true
127+
}
128+
if strings.Contains(s, "access") {
129+
config.AccessLog = true
130+
}
132131
}
133132
if properties.On(false, "http.body.disabled") {
134133
config.Body = false

logger/httpretty/printer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,11 @@ func (p *printer) printRequestHeader(req *http.Request) {
624624
}
625625
scheme := req.URL.Scheme
626626
if scheme == "" {
627-
scheme = "https"
627+
if req.TLS != nil {
628+
scheme = "https"
629+
} else {
630+
scheme = "http"
631+
}
628632
}
629633
uri = fmt.Sprintf("%s://%s%s", scheme, host, req.URL.RequestURI())
630634
}

0 commit comments

Comments
 (0)