-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhandler_test.go
More file actions
43 lines (35 loc) · 1.03 KB
/
handler_test.go
File metadata and controls
43 lines (35 loc) · 1.03 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
package slogcolor_test
import (
"errors"
"log/slog"
"os"
"testing"
"time"
"github.com/SladkyCitron/slogcolor"
"github.com/fatih/color"
)
func Example() {
opts := slogcolor.DefaultOptions
opts.Level = slog.LevelDebug
opts.LevelTags = map[slog.Level]string{
slog.LevelInfo: color.New(color.BgCyan, color.FgBlack).Sprint("Info"),
}
slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, opts)))
slog.Info("Initializing")
slog.Debug("Init done", "duration", 500*time.Millisecond)
slog.Warn("Slow request!", "method", "GET", "path", "/api/users", "duration", 750*time.Millisecond)
slog.Error("DB connection lost!", "err", errors.New("connection reset"), "db", "horalky")
// Output:
opts.NoTime = true
slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, opts)))
slog.Info("Message without time")
}
func BenchmarkLog(b *testing.B) {
b.StopTimer()
l := slog.New(slogcolor.NewHandler(os.Stderr, slogcolor.DefaultOptions))
for i := 0; i < b.N; i++ {
b.StartTimer()
l.Info("benchmarking", "i", i)
b.StopTimer()
}
}