Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve slog coverage #1347

Merged
merged 1 commit into from Sep 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 78 additions & 0 deletions exp/zapslog/handler_test.go
Expand Up @@ -25,6 +25,7 @@ package zapslog
import (
"log/slog"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -69,6 +70,20 @@ func TestAddStack(t *testing.T) {
)
}

func TestAddStackSkip(t *testing.T) {
fac, logs := observer.New(zapcore.DebugLevel)
sl := slog.New(NewHandler(fac, AddStacktraceAt(slog.LevelDebug), WithCallerSkip(1)))
sl.Info("msg")

require.Len(t, logs.AllUntimed(), 1, "Expected exactly one entry to be logged")
entry := logs.AllUntimed()[0]
assert.Regexp(t,
`src/testing/testing.go:\d+`,
entry.Stack,
"Unexpected stack trace annotation.",
)
}

func TestEmptyAttr(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -99,3 +114,66 @@ func TestEmptyAttr(t *testing.T) {
}, logs[0].ContextMap(), "Unexpected context")
})
}

func TestWithName(t *testing.T) {
t.Parallel()
fac, observedLogs := observer.New(zapcore.DebugLevel)
t.Run("default", func(t *testing.T) {
sl := slog.New(NewHandler(fac))
sl.Info("msg")

logs := observedLogs.TakeAll()
require.Len(t, logs, 1, "Expected exactly one entry to be logged")
entry := logs[0]
assert.Equal(t, "", entry.LoggerName, "Unexpected logger name")
})
t.Run("with name", func(t *testing.T) {
sl := slog.New(NewHandler(fac, WithName("test-name")))
sl.Info("msg")

logs := observedLogs.TakeAll()
require.Len(t, logs, 1, "Expected exactly one entry to be logged")
entry := logs[0]
assert.Equal(t, "test-name", entry.LoggerName, "Unexpected logger name")
})
}

type Token string

func (Token) LogValue() slog.Value {
return slog.StringValue("REDACTED_TOKEN")
}

func TestAttrKinds(t *testing.T) {
fac, logs := observer.New(zapcore.DebugLevel)
sl := slog.New(NewHandler(fac))
testToken := Token("no")
sl.Info(
"msg",
slog.Bool("bool", true),
slog.Duration("duration", time.Hour),
slog.Float64("float64", 42.0),
slog.Int64("int64", -1234),
slog.Time("time", time.Date(2015, 10, 21, 7, 28, 00, 0, time.UTC)),
slog.Uint64("uint64", 2),
slog.Group("group", slog.String("inner", "inner-group")),
"logvaluer", testToken,
"any", "what am i?",
)

require.Len(t, logs.AllUntimed(), 1, "Expected exactly one entry to be logged")
entry := logs.AllUntimed()[0]
assert.Equal(t,
map[string]any{
"bool": true,
"duration": time.Hour,
"float64": float64(42),
"group": map[string]any{"inner": "inner-group"},
"int64": int64(-1234),
"time": time.Date(2015, time.October, 21, 7, 28, 0, 0, time.UTC),
"uint64": uint64(2),
"logvaluer": "REDACTED_TOKEN",
"any": "what am i?",
},
entry.ContextMap())
}