-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathlogger_levelgate_test.go
More file actions
44 lines (35 loc) · 1.17 KB
/
Copy pathlogger_levelgate_test.go
File metadata and controls
44 lines (35 loc) · 1.17 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
package slog_test
import (
"io"
"sync/atomic"
"testing"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/slog"
"github.com/gookit/slog/handler"
)
type countStringer struct{ n *int32 }
func (c countStringer) String() string { atomic.AddInt32(c.n, 1); return "X" }
// regression: a disabled level must not build/format the message at all.
func TestLogger_DisabledLevel_SkipsFormatting(t *testing.T) {
logger := slog.NewWithHandlers(
handler.NewIOWriter(io.Discard, []slog.Level{slog.ErrorLevel}),
)
var cnt int32
for i := 0; i < 100; i++ {
logger.Info("msg", countStringer{&cnt}) // Info is filtered out
}
assert.Eq(t, int32(0), atomic.LoadInt32(&cnt))
// the enabled level still formats
logger.Error("msg", countStringer{&cnt})
assert.Eq(t, int32(1), atomic.LoadInt32(&cnt))
}
// Panic/Fatal must still trigger their side effects even with no matching handler.
func TestLogger_PanicFatal_AlwaysHandled_NoMatchingHandler(t *testing.T) {
logger := slog.NewWithHandlers(
handler.NewIOWriter(io.Discard, []slog.Level{slog.InfoLevel}),
)
panicked := false
logger.PanicFunc = func(v any) { panicked = true }
logger.Panic("boom")
assert.True(t, panicked)
}