-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
middleware_formatter_flatten.go
149 lines (127 loc) · 4.35 KB
/
middleware_formatter_flatten.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package slogformatter
import (
"context"
"log/slog"
"github.com/samber/lo"
slogmulti "github.com/samber/slog-multi"
)
type FlattenFormatterMiddlewareOptions struct {
// Ignore attribute path and therefore ignore attribute key prefix.
// Some attribute keys may collide.
IgnorePath bool
// Separator between prefix and key.
Separator string
// Attribute key prefix.
Prefix string
}
// NewFlattenFormatterMiddlewareOptions returns a formatter middleware that flatten attributes recursively.
func (o FlattenFormatterMiddlewareOptions) NewFlattenFormatterMiddlewareOptions() slogmulti.Middleware {
return func(next slog.Handler) slog.Handler {
return &FlattenFormatterMiddleware{
next: next,
option: FlattenFormatterMiddlewareOptions{
IgnorePath: o.IgnorePath,
Separator: lo.Ternary(o.Separator == "", ".", o.Separator),
Prefix: o.Prefix,
},
}
}
}
var _ slog.Handler = (*FlattenFormatterMiddleware)(nil)
type FlattenFormatterMiddleware struct {
next slog.Handler
option FlattenFormatterMiddlewareOptions
}
// Implements slog.Handler
func (h *FlattenFormatterMiddleware) Enabled(ctx context.Context, level slog.Level) bool {
return h.next.Enabled(ctx, level)
}
// Implements slog.Handler
func (h *FlattenFormatterMiddleware) Handle(ctx context.Context, record slog.Record) error {
// newRecord := slog.NewRecord(record.Time, record.Level, record.Message, record.PC)
// record.Attrs(func(attr slog.Attr) bool {
// if !h.option.IgnorePath {
// newRecord.AddAttrs(
// PrefixAttrKeys(
// lo.Ternary(h.option.Prefix == "" || h.option.IgnorePath, "", h.option.Prefix+h.option.Separator),
// FlattenAttrsWithPrefix(h.option.Separator, h.option.Prefix, []slog.Attr{attr}),
// )...,
// )
// } else {
// newRecord.AddAttrs(FlattenAttrs([]slog.Attr{attr})...)
// }
// return true
// })
// return h.next.Handle(ctx, newRecord)
return h.next.Handle(ctx, record)
}
// Implements slog.Handler
func (h *FlattenFormatterMiddleware) WithAttrs(attrs []slog.Attr) slog.Handler {
if !h.option.IgnorePath {
attrs = PrefixAttrKeys(
lo.Ternary(h.option.Prefix == "" || h.option.IgnorePath, "", h.option.Prefix+h.option.Separator),
FlattenAttrsWithPrefix(h.option.Separator, h.option.Prefix, attrs),
)
} else {
attrs = FlattenAttrs(attrs)
}
return &FlattenFormatterMiddleware{
next: h.next.WithAttrs(attrs),
option: h.option,
}
}
// Implements slog.Handler
func (h *FlattenFormatterMiddleware) WithGroup(name string) slog.Handler {
prefix := h.option.Prefix + h.option.Separator
if h.option.IgnorePath || h.option.Prefix == "" {
prefix = ""
}
return &FlattenFormatterMiddleware{
next: h.next,
option: FlattenFormatterMiddlewareOptions{
IgnorePath: h.option.IgnorePath,
Separator: h.option.Separator,
Prefix: prefix + name,
},
}
}
// PrefixAttrKeys prefix attribute keys.
func PrefixAttrKeys(prefix string, attrs []slog.Attr) []slog.Attr {
return lo.Map(attrs, func(item slog.Attr, _ int) slog.Attr {
return slog.Attr{
Key: prefix + item.Key,
Value: item.Value,
}
})
}
// FlattenAttrs flatten attributes recursively.
func FlattenAttrs(attrs []slog.Attr) []slog.Attr {
output := []slog.Attr{}
for _, attr := range attrs {
switch attr.Value.Kind() {
case slog.KindAny, slog.KindBool, slog.KindDuration, slog.KindFloat64, slog.KindInt64, slog.KindUint64, slog.KindString, slog.KindTime:
output = append(output, attr)
case slog.KindGroup:
output = append(output, attr.Value.Group()...)
case slog.KindLogValuer:
output = append(output, slog.Any(attr.Key, attr.Value.Resolve().Any()))
}
}
return output
}
// FlattenAttrsWithPrefix flatten attributes recursively, with prefix.
func FlattenAttrsWithPrefix(separator string, prefix string, attrs []slog.Attr) []slog.Attr {
output := []slog.Attr{}
for _, attr := range attrs {
switch attr.Value.Kind() {
case slog.KindAny, slog.KindBool, slog.KindDuration, slog.KindFloat64, slog.KindInt64, slog.KindUint64, slog.KindString, slog.KindTime:
output = append(output, attr)
case slog.KindGroup:
output = append(output, PrefixAttrKeys(prefix+separator+attr.Key+separator, FlattenAttrsWithPrefix(separator, "", attr.Value.Group()))...)
case slog.KindLogValuer:
attr := slog.Any(attr.Key, attr.Value.Resolve().Any())
output = append(output, FlattenAttrsWithPrefix(separator, prefix, []slog.Attr{attr})...)
}
}
return output
}