Skip to content

Commit 9c61bd8

Browse files
taosoAlexVulaj
authored andcommitted
Support logging Authorization User
For reqeust with Authorization header or Proxy-Authorization header, the user name is not stored in the URL. So the logger will not output it. In order to log user name for Authorization, we could update the User filed of http.Request. So the logger should use the latest value.
1 parent 02b3ca3 commit 9c61bd8

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

logging.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ func (h loggingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
5252
}
5353
}
5454

55+
if url.User != req.URL.User {
56+
url.User = req.URL.User
57+
}
58+
5559
params := LogFormatterParams{
5660
Request: req,
5761
URL: url,

logging_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ func TestLogPathRewrites(t *testing.T) {
129129
}
130130
}
131131

132+
func TestLogUser(t *testing.T) {
133+
var buf bytes.Buffer
134+
135+
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
136+
req.URL.User = url.User("foo")
137+
w.WriteHeader(http.StatusOK)
138+
})
139+
logger := LoggingHandler(&buf, handler)
140+
141+
logger.ServeHTTP(httptest.NewRecorder(), newRequest(http.MethodGet, "/"))
142+
143+
if !strings.Contains(buf.String(), "- foo [") {
144+
t.Fatalf("Got log %#v, wanted substring %#v", buf.String(), "- foo [")
145+
}
146+
}
147+
132148
func BenchmarkWriteLog(b *testing.B) {
133149
loc, err := time.LoadLocation("Europe/Warsaw")
134150
if err != nil {

0 commit comments

Comments
 (0)