-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
49 lines (37 loc) · 1.13 KB
/
main.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
package main
import (
"context"
"github.com/elgopher/yala/adapter/console"
"github.com/elgopher/yala/logger"
)
// This example shows how to rename all fields matching given string.
func main() {
ctx := context.Background()
adapter := RenameFieldsAdapter{
From: "this",
To: "that",
NextAdapter: console.StdoutAdapter(),
}
log := logger.WithAdapter(adapter)
log.InfoFields(ctx, "this field will be replaced with that", logger.Fields{
"this": "value",
})
}
// RenameFieldsAdapter is a middleware (decorator) renaming all fields equal to From into To.
type RenameFieldsAdapter struct {
From, To string
NextAdapter logger.Adapter
}
func (r RenameFieldsAdapter) Log(ctx context.Context, entry logger.Entry) {
fields := make([]logger.Field, len(entry.Fields)) // Create a new slice in order to be concurrency-safe
for i, field := range entry.Fields {
key := field.Key
if key == r.From {
key = r.To
}
fields[i] = logger.Field{Key: key, Value: field.Value}
}
entry.Fields = fields
entry.SkippedCallerFrames // each middleware adapter must additionally skip one frame
r.NextAdapter.Log(ctx, entry)
}