From 14b930781e034fb67e9e7e6e5cb82f6cf4b1e53f Mon Sep 17 00:00:00 2001 From: Evan Reichard Date: Tue, 9 Jan 2024 21:36:36 -0500 Subject: [PATCH] [add] username in http access logs --- api/api.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/api/api.go b/api/api.go index fcd3e5d..667b238 100644 --- a/api/api.go +++ b/api/api.go @@ -247,8 +247,26 @@ func apiLogger() gin.HandlerFunc { endTime := time.Now() latency := endTime.Sub(startTime).Round(time.Microsecond) + // Get Username + var auth authData + if data, _ := c.Get("Authorization"); data != nil { + auth = data.(authData) + } + + username := auth.UserName + if username != "" { + username = " (" + username + ")" + } + // Log Result - log.Infof("[HTTPRouter] %-15s (%10s) %d %7s %s", c.ClientIP(), latency, c.Writer.Status(), c.Request.Method, c.Request.URL.Path) + log.Infof("[HTTPRouter] %-15s (%10s) %d %7s %s%s", + c.ClientIP(), + latency, + c.Writer.Status(), + c.Request.Method, + c.Request.URL.Path, + username, + ) } }