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
funchelloUser(whttp.ResponseWriter, r *http.Request) {
536
+
// Stand-in for a User ID.
537
+
// Add it to our middleware's context
538
+
id:= r.URL.Query().Get("id")
539
+
540
+
// sloghttp.With will add the "id" to to the middleware, because it is a
541
+
// synchronized map. It will show up in all log calls up and down the stack,
542
+
// until the request sloghttp middleware exits.
543
+
ctx:= sloghttp.With(r.Context(), "id", id)
544
+
545
+
// The regular slogctx.With will add "foo" only to the Returned context,
546
+
// which will limits its scope to the rest of this function (helloUser) and
547
+
// any functions called by helloUser and passed this context.
548
+
// The original caller of helloUser and all the middlewares will NOT see
549
+
// "foo", because it is only part of the newly returned ctx.
550
+
ctx = slogctx.With(ctx, "foo", "bar")
551
+
552
+
// Log some things.
553
+
// Should also have both "path", "id", and "foo"
554
+
slogctx.Info(ctx, "saying hello...")
555
+
/*
556
+
{
557
+
"time": "2024-04-01T00:06:11Z",
558
+
"level": "INFO",
559
+
"msg": "saying hello...",
560
+
"path": "/hello",
561
+
"id": "24680",
562
+
"foo": "bar"
563
+
}
564
+
*/
565
+
566
+
// Response
567
+
_, _ = w.Write([]byte("Hello User #" + id))
568
+
}
569
+
```
570
+
446
571
### slog-multi Middleware
447
572
This library has a convenience method that allow it to interoperate with [github.com/samber/slog-multi](https://github.com/samber/slog-multi),
448
573
in order to easily setup slog workflows such as pipelines, fanout, routing, failover, etc.
0 commit comments