Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
63 lines (54 loc) · 1.88 KB
/
Copy pathmain.go
File metadata and controls
63 lines (54 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// basic demonstrates core slog-schema features: struct tags, embedded structs,
// omitempty, slices, maps, and nested structs.
package main
import (
"log/slog"
"os"
"time"
slogschema "github.com/silvercory/slog-schema"
)
// Meta is an embedded struct — its fields are inlined into the parent's log output.
type Meta struct {
TraceID string `slog:"trace_id"`
Region string `slog:"region"`
}
// Address is a nested struct — it appears as a slog group.
type Address struct {
Street string `slog:"street"`
City string `slog:"city"`
Post string `slog:"post_code"`
}
type Request struct {
Meta // inlined
Method string `slog:"method"`
Path string `slog:"path"`
Status int `slog:"status"`
Duration time.Duration `slog:"duration"`
Error string `slog:"error,omitempty"` // omitted when empty
Tags []string `slog:"tags"`
Headers map[string]string `slog:"headers"`
Addr Address `slog:"addr"`
Internal string // no slog tag — skipped entirely
Secret string `slog:"-"` // explicitly skipped
}
func main() {
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
// Error is empty — omitempty suppresses it.
req := Request{
Meta: Meta{TraceID: "abc-123", Region: "eu-west-1"},
Method: "POST",
Path: "/api/orders",
Status: 201,
Duration: 18 * time.Millisecond,
Tags: []string{"orders", "v2"},
Headers: map[string]string{"content-type": "application/json"},
Addr: Address{Street: "1 High St", City: "London", Post: "EC1A 1BB"},
Internal: "not logged",
Secret: "not logged either",
}
logger.LogAttrs(nil, slog.LevelInfo, "request", slogschema.ToAttrs(req)...)
// With an error present.
req.Status = 500
req.Error = "upstream timeout"
logger.LogAttrs(nil, slog.LevelError, "request", slogschema.ToAttrs(req)...)
}