Improve logging

This commit is contained in:
viktorstrate
2020-02-21 21:00:40 +01:00
parent 935dc5b55e
commit 37aea1fb3a
3 changed files with 17 additions and 2 deletions

View File

@@ -37,13 +37,26 @@ func LoggingMiddleware(next http.Handler) http.Handler {
statusColor = color.Colorize("r")
}
method := r.Method
var methodColor string
switch {
case method == http.MethodGet:
methodColor = color.Colorize("b")
case method == http.MethodPost:
methodColor = color.Colorize("g")
case method == http.MethodOptions:
methodColor = color.Colorize("y")
default:
methodColor = color.Colorize("r")
}
user := auth.UserFromContext(r.Context())
userText := "unauthenticated"
if user != nil {
userText = color.Sprintf("@ruser: %s", user.Username)
}
statusText := color.Sprintf("%s%s %d", statusColor, r.Method, status)
statusText := color.Sprintf("%s%s %s%d", methodColor, r.Method, statusColor, status)
requestText := fmt.Sprintf("%s%s", r.Host, r.URL.Path)
durationText := color.Sprintf("@c%s", elapsed)