-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_test.go
127 lines (110 loc) · 3.49 KB
/
logging_test.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
package logging_test
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"log/slog"
"os"
"testing"
"time"
"github.com/castai/logging"
"github.com/stretchr/testify/require"
"golang.org/x/time/rate"
)
func TestExampleLogger(t *testing.T) {
text := logging.NewTextHandler(logging.TextHandlerConfig{
Level: logging.MustParseLevel("INFO"),
Output: os.Stdout,
AddSource: false,
})
rt := logging.NewRateLimitHandler(logging.RateLimiterHandlerConfig{
Limit: rate.Limit(100 * time.Millisecond),
Burst: 100,
})
export := logging.NewExportHandler(logging.ExportHandlerConfig{
MinLevel: logging.MustParseLevel("WARN"),
BufferSize: 1000,
})
log := logging.New(text, export, rt)
// Print dropped logs due rate limit.
go logging.PrintDroppedLogs(context.Background(), 5*time.Second, rt, func(level slog.Level, count uint64) {
fmt.Println("dropped lines", level, count)
})
// Export logs to your destination.
go func() {
select {
case log := <-export.Records():
fmt.Println(log)
}
}()
log.Infof("debug message with format value %s", "hello")
log.WithField("component", "agent").Errorf("something failed: %v", "unknown")
}
func TestLogger(t *testing.T) {
t.Run("print logs with default options", func(t *testing.T) {
log := logging.New()
log.Errorf("something wrong: %v", errors.New("ups"))
serverLog := log.WithField("component", "server")
serverLog.Info("with component")
serverLog.Info("more server logs")
})
t.Run("print logs with text handler", func(t *testing.T) {
r := require.New(t)
var buf bytes.Buffer
log := logging.New(logging.NewTextHandler(logging.TextHandlerConfig{
Level: logging.MustParseLevel("DEBUG"),
Output: io.MultiWriter(&buf, os.Stdout),
AddSource: false,
}))
log.Errorf("something wrong: %v", errors.New("ups"))
serverLog := log.WithField("component", "server")
serverLog.Info("with component")
serverLog.Info("more server logs")
r.Contains(buf.String(), `level=ERROR msg="something wrong: ups"`)
r.Contains(buf.String(), `level=INFO msg="with component" component=server`)
r.Contains(buf.String(), `level=INFO msg="more server logs" component=server`)
})
t.Run("chain handlers", func(t *testing.T) {
r := require.New(t)
var buf bytes.Buffer
opts := []logging.Handler{
logging.NewTextHandler(logging.TextHandlerConfig{
Output: io.MultiWriter(&buf, os.Stdout),
Level: logging.MustParseLevel("DEBUG"),
}),
logging.HandlerFunc(func(next slog.Handler) slog.Handler {
return &customHandler{name: "custom 1", next: next}
}),
logging.HandlerFunc(func(next slog.Handler) slog.Handler {
return &customHandler{name: "custom 2", next: next}
}),
logging.HandlerFunc(func(next slog.Handler) slog.Handler {
return &customHandler{name: "custom 3", next: next}
}),
}
log := logging.New(opts...)
log.Info("msg")
log.WithField("k", "v").Debug("msg2")
log.WithGroup("group").Debug("msg3")
r.Contains(buf.String(), `level=INFO msg="msg custom 3 custom 2 custom 1"`)
})
}
type customHandler struct {
name string
next slog.Handler
}
func (c *customHandler) Enabled(ctx context.Context, level slog.Level) bool {
return c.next.Enabled(ctx, level)
}
func (c *customHandler) Handle(ctx context.Context, record slog.Record) error {
record.Message += " " + c.name
return c.next.Handle(ctx, record)
}
func (c *customHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return c.next.WithAttrs(attrs)
}
func (c *customHandler) WithGroup(name string) slog.Handler {
return c.next.WithGroup(name)
}