You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've implemented an RequestID middleware, so that I can log an UUID for each Request in my logs.
The problem is that when I don't use the logger in the context, and use the global zerolog Logger a %!s(<nil>) string is printed. I expected it to not log anything for the UUID part, since it's not set in the context of the global logger.
func RequestIDMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqID := uuid.New().String()
ctx := context.WithValue(r.Context(), service.RequestIDKey, reqID)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func LoggerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqId, ok := r.Context().Value(service.RequestIDKey).(string)
if !ok {
reqId = uuid.New().String()
}
l := log.Logger.With().Str("request_id", reqId).Logger()
r = r.WithContext(l.WithContext(r.Context()))
next.ServeHTTP(w, r)
})
}
func main() {
l := logger.NewLogger()
log.Logger = l
zerolog.DefaultContextLogger = &l
s := service.NewService()
h := handler.NewHandler(s)
router := mux.NewRouter()
router.Handle("/context-logger-message", RequestIDMiddleware(LoggerMiddleware(
http.HandlerFunc(h.MessageWithLoggerInContext)))).Methods("GET")
log.Info().Msg("Starting server")
if err := http.ListenAndServe(":8080", router); err != nil {
log.Fatal().Err(err).Msg("Server failed")
}
}
When I start the application I receive the following log message:
2025-02-23T00:39:35+02:00 INF D:/dev/git/trainings/golang/request_id_logging/main.go:26 %!s(<nil>) > Starting server
From what I understand, the global logger should not contain the context data, for the request ID and not print the %!s(<nil>) string.
Hitting the HTTP Request, works, and logs a proper log with an UUID part.
The text was updated successfully, but these errors were encountered:
vkosev
changed the title
Global logger logging context value, even tough it such an value in the context should not exist.
Global logger logging context value, even tough a key does not exist in the event map.
Feb 22, 2025
I've implemented an RequestID middleware, so that I can log an UUID for each Request in my logs.
The problem is that when I don't use the logger in the context, and use the global zerolog Logger a
%!s(<nil>)
string is printed. I expected it to not log anything for the UUID part, since it's not set in the context of the global logger.These are my middleware funcs
When I start the application I receive the following log message:
From what I understand, the global logger should not contain the context data, for the request ID and not print the
%!s(<nil>)
string.Hitting the HTTP Request, works, and logs a proper log with an UUID part.
The text was updated successfully, but these errors were encountered: