| 1 |
//go:build go1.21 |
| 2 |
|
| 3 |
package logger |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"context" |
| 8 |
"log/slog" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
) |
| 13 |
|
| 14 |
func TestSlogLogger(t *testing.T) { |
| 15 |
buf := &bytes.Buffer{} |
| 16 |
handler := slog.NewTextHandler(buf, &slog.HandlerOptions{AddSource: true}) |
| 17 |
logger := NewSlogLogger(slog.New(handler), Config{LogLevel: Info}) |
| 18 |
|
| 19 |
logger.Trace(context.Background(), time.Now(), func() (string, int64) { |
| 20 |
return "select count(*) from users", 0 |
| 21 |
}, nil) |
| 22 |
|
| 23 |
if strings.Contains(buf.String(), "gorm/logger/slog.go") { |
| 24 |
t.Error("Found internal slog.go reference in caller frame. Expected only test file references.") |
| 25 |
} |
| 26 |
|
| 27 |
if !strings.Contains(buf.String(), "gorm/logger/slog_test.go") { |
| 28 |
t.Error("Missing expected test file reference. 'gorm/logger/slog_test.go' should appear in caller frames.") |
| 29 |
} |
| 30 |
} |