Standard library / log/slog
slog.JSONHandler.WithGroup()
Return a handler qualifying subsequent attributes with a group name. Expected stdout: '{"level":"INFO","msg":"ready","demo":{"n":2}}\n'.
Formal Syntax & Structure
go syntaxgo
func (h *JSONHandler) WithGroup(name string) HandlerParameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | string | Group name. |
Return Value
Handler
Return a handler qualifying subsequent attributes with a group name.
Working Example
Example Snippetgo
package main
import ( "bytes" "context" "fmt" "log/slog" "time")
func check(e error) { if e != nil { panic(e) }}
func main() { var b bytes.Buffer h := slog.NewJSONHandler(&b, nil) child := h.WithGroup("demo") r := slog.NewRecord(time.Time{}, slog.LevelInfo, "ready", 0) r.Add("n", 2) check(child.Handle(context.Background(), r)) fmt.Print(b.String())}