Skip to content

Commit

Permalink
Use existing ctx logger if available
Browse files Browse the repository at this point in the history
  • Loading branch information
irees committed Dec 12, 2024
1 parent 4e3adcc commit b7e4cf5
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lmw.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ import (
func LoggingMiddleware(longQueryDuration int, getUserName func(context.Context) string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Setup context logger
// Get context logger
ctx := r.Context()
rlog := For(ctx).With()

// Add username
rlog := Logger.With()
if getUserName != nil {
if u := getUserName(r.Context()); u != "" {
rlog = rlog.Str("user", getUserName(r.Context()))
if u := getUserName(ctx); u != "" {
rlog = rlog.Str("user", getUserName(ctx))
}
}
// Add request ID
// TODO

// Set as context logger
rlogger := rlog.Logger()
r = r.WithContext(WithLogger(r.Context(), rlogger))
ctx = WithLogger(ctx, rlogger)
r = r.WithContext(ctx)

// Get request body for logging if request is json and length under 20kb
t1 := time.Now()
Expand Down

0 comments on commit b7e4cf5

Please sign in to comment.