-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvalue_formatter_test.go
More file actions
60 lines (50 loc) · 1.39 KB
/
Copy pathvalue_formatter_test.go
File metadata and controls
60 lines (50 loc) · 1.39 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package slogcolor_test
import (
"bytes"
"log/slog"
"strings"
"testing"
"time"
"github.com/SladkyCitron/slogcolor"
"github.com/fatih/color"
)
func TestHandlerUsesCustomValueFormatter(t *testing.T) {
var buf bytes.Buffer
h := slogcolor.NewHandler(&buf, &slogcolor.Options{
Level: slog.LevelDebug,
NoColor: true,
NoTime: true,
SrcFileMode: slogcolor.Nop,
MsgPrefix: "",
MsgColor: color.New(),
ValueFormatter: func(v slog.Value) string {
return "custom(" + v.String() + ")"
},
})
logger := slog.New(h)
logger.Info("hello", "duration", 1500*time.Millisecond, "count", 7)
out := buf.String()
if !strings.Contains(out, "duration=custom(1.5s)") {
t.Fatalf("expected custom formatter output for duration, got: %q", out)
}
if !strings.Contains(out, "count=custom(7)") {
t.Fatalf("expected custom formatter output for int, got: %q", out)
}
}
func TestHandlerUsesDefaultValueFormattingWhenFormatterNil(t *testing.T) {
var buf bytes.Buffer
h := slogcolor.NewHandler(&buf, &slogcolor.Options{
Level: slog.LevelDebug,
NoColor: true,
NoTime: true,
SrcFileMode: slogcolor.Nop,
MsgPrefix: "",
MsgColor: color.New(),
})
logger := slog.New(h)
logger.Info("hello", "duration", 1500*time.Millisecond)
out := buf.String()
if !strings.Contains(out, "duration=1.5s") {
t.Fatalf("expected default value string formatting, got: %q", out)
}
}