-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
45 lines (36 loc) · 1.15 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
package main
import (
"context"
"strings"
"github.com/elgopher/yala/adapter/console"
"github.com/elgopher/yala/logger"
)
// This advanced example shows how to filter out messages starting with given prefix
func main() {
adapter := console.StdoutAdapter()
// creates an adapter which filters out messages
filterAdapter := FilterOutMessages{
Prefix: "example:",
NextAdapter: adapter,
}
l := logger.WithAdapter(filterAdapter)
ctx := context.Background()
// The chain of execution will look like this:
// l.Info() -> FilterOutMessages -> console adapter
l.Info(ctx, "message without prefix")
l.Info(ctx, "example: message which will be filtered out")
l.Info(ctx, "another message without prefix")
}
// FilterOutMessages is a middleware (decorator) which filters out entries
// with message starting with prefix
type FilterOutMessages struct {
Prefix string
NextAdapter logger.Adapter
}
func (a FilterOutMessages) Log(ctx context.Context, entry logger.Entry) {
if strings.HasPrefix(entry.Message, a.Prefix) {
return
}
entry.SkippedCallerFrames // each middleware adapter must additionally skip one frame
a.NextAdapter.Log(ctx, entry)
}