# How to ## At the middleware ```go func (h *Handler) LogMW(next http.HandlerFunc) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var ( ctx context.Context = rctx.AssignRequestID(r.Context()) rWithCtx *http.Request = r.WithContext(ctx) reqID string = rctx.GetRequestID(ctx) values = r.URL.Query() attrs []slog.Attr = []slog.Attr{ slog.String("req id", reqID), slog.String("method", r.Method), slog.String("URI", r.RequestURI), slog.String("remote", r.RemoteAddr), } ) // Append query params as logger info: for i, value := range values { switch { case len(value) == 1: attrs = append(attrs, slog.Attr{ Key: i, Value: slog.StringValue(value[0]), }) case len(value) > 1: attrs = append(attrs, slog.Attr{ Key: i, Value: slog.AnyValue(value), }) } } h.log.LogAttrs( rWithCtx.Context(), slog.LevelInfo, "request called", attrs..., ) next.ServeHTTP(w, rWithCtx) slog.Debug("request handled", slog.String("request ID", reqID)) }) } ``` ## At the http Handler ```go func HandleSomething(w http.ResponseWriter, r *http.Request) { var ( ctx context.Context = r.Context() reqID string = rctx.GetRequestID(ctx) log *slog.Logger = slog.Default().With( slog.String("req id", reqID), slog.String("level", "handler"), ) ) // do something and where is the case to log w/ req id: log.Error("decode req body", slog.String("error", err.Error())) return } ```