-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_test.go
More file actions
133 lines (114 loc) · 3.55 KB
/
Copy pathlog_test.go
File metadata and controls
133 lines (114 loc) · 3.55 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
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
128
129
130
131
132
133
package outrunner
import (
"bytes"
"context"
"log/slog"
"strings"
"testing"
"time"
)
func TestSimpleHandlerOutput(t *testing.T) {
var buf bytes.Buffer
h := NewSimpleHandler(&buf, slog.LevelInfo)
logger := slog.New(h)
ts := time.Date(2026, 3, 30, 14, 5, 9, 0, time.UTC)
r := slog.NewRecord(ts, slog.LevelInfo, "hello world", 0)
if err := h.Handle(context.Background(), r); err != nil {
t.Fatal(err)
}
got := buf.String()
if !strings.Contains(got, "2026-03-30 14:05:09") {
t.Errorf("expected timestamp, got %q", got)
}
if !strings.Contains(got, "INFO") {
t.Errorf("expected INFO level, got %q", got)
}
if !strings.Contains(got, "hello world") {
t.Errorf("expected message, got %q", got)
}
// Verify it ends with a newline
if !strings.HasSuffix(got, "\n") {
t.Errorf("expected trailing newline, got %q", got)
}
// Now test with attrs
buf.Reset()
logger.Info("loaded", slog.Int("runners", 3), slog.String("file", "config.yml"))
got = buf.String()
if !strings.Contains(got, "runners=3") {
t.Errorf("expected runners=3, got %q", got)
}
if !strings.Contains(got, "file=config.yml") {
t.Errorf("expected file=config.yml, got %q", got)
}
}
func TestSimpleHandlerLevelFiltering(t *testing.T) {
var buf bytes.Buffer
h := NewSimpleHandler(&buf, slog.LevelWarn)
if h.Enabled(context.Background(), slog.LevelDebug) {
t.Error("DEBUG should be disabled at WARN level")
}
if h.Enabled(context.Background(), slog.LevelInfo) {
t.Error("INFO should be disabled at WARN level")
}
if !h.Enabled(context.Background(), slog.LevelWarn) {
t.Error("WARN should be enabled at WARN level")
}
if !h.Enabled(context.Background(), slog.LevelError) {
t.Error("ERROR should be enabled at WARN level")
}
}
func TestSimpleHandlerWithGroup(t *testing.T) {
var buf bytes.Buffer
h := NewSimpleHandler(&buf, slog.LevelInfo)
logger := slog.New(h).WithGroup("scaler")
logger.Info("started", slog.String("name", "test-runner"))
got := buf.String()
if !strings.Contains(got, "scaler.name=test-runner") {
t.Errorf("expected grouped attr, got %q", got)
}
}
func TestSimpleHandlerNestedGroups(t *testing.T) {
var buf bytes.Buffer
h := NewSimpleHandler(&buf, slog.LevelInfo)
logger := slog.New(h).WithGroup("docker").WithGroup("container")
logger.Info("created", slog.String("id", "abc123"))
got := buf.String()
if !strings.Contains(got, "docker.container.id=abc123") {
t.Errorf("expected nested groups, got %q", got)
}
}
func TestSimpleHandlerWithAttrs(t *testing.T) {
var buf bytes.Buffer
h := NewSimpleHandler(&buf, slog.LevelInfo)
logger := slog.New(h).With(slog.String("scaleSet", "linux"))
logger.Info("ready")
got := buf.String()
if !strings.Contains(got, "scaleSet=linux") {
t.Errorf("expected pre-set attr, got %q", got)
}
// Pre-set attrs should appear on every message
buf.Reset()
logger.Info("second")
got = buf.String()
if !strings.Contains(got, "scaleSet=linux") {
t.Errorf("expected pre-set attr on second message, got %q", got)
}
}
func TestSimpleHandlerEmptyAttr(t *testing.T) {
var buf bytes.Buffer
h := NewSimpleHandler(&buf, slog.LevelInfo)
r := slog.NewRecord(time.Now(), slog.LevelInfo, "test", 0)
r.AddAttrs(slog.Attr{}) // empty attr should be skipped
r.AddAttrs(slog.String("key", "val"))
if err := h.Handle(context.Background(), r); err != nil {
t.Fatal(err)
}
got := buf.String()
if !strings.Contains(got, "key=val") {
t.Errorf("expected key=val, got %q", got)
}
// Should not have extra spaces from empty attr
if strings.Contains(got, " ") {
t.Errorf("unexpected double space (empty attr not skipped), got %q", got)
}
}