Skip to content

Commit

Permalink
feat: add more prominent failure notice on slogtest error
Browse files Browse the repository at this point in the history
Signed-off-by: Spike Curtis <[email protected]>
  • Loading branch information
spikecurtis committed Aug 31, 2023
1 parent b386d5d commit e195bb8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
19 changes: 10 additions & 9 deletions sloggers/slogtest/t.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package slogtest // import "cdr.dev/slog/sloggers/slogtest"

import (
"context"
"fmt"
"log"
"os"
"strings"
Expand Down Expand Up @@ -78,19 +79,19 @@ func (ts *testSink) LogEntry(ctx context.Context, ent slog.SinkEntry) {
// The testing package logs to stdout and not stderr.
entryhuman.Fmt(&sb, os.Stdout, ent)

s := sb.String()

switch ent.Level {
case slog.LevelDebug, slog.LevelInfo, slog.LevelWarn:
ts.tb.Log(s)
case slog.LevelError, slog.LevelCritical:
if ts.opts.IgnoreErrors {
ts.tb.Log(s)
ts.tb.Log(sb.String())
case slog.LevelError, slog.LevelCritical, slog.LevelFatal:
if ts.opts.IgnoreErrors && ent.Level != slog.LevelFatal {
ts.tb.Log(sb.String())
} else {
ts.tb.Error(s)
sb.WriteString(fmt.Sprintf(
"\n *** slogtest: log detected at level %s; TEST FAILURE ***",
ent.Level,
))
ts.tb.Error(sb.String())
}
case slog.LevelFatal:
ts.tb.Fatal(s)
}
}

Expand Down
2 changes: 1 addition & 1 deletion sloggers/slogtest/t_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestCleanup(t *testing.T) {
fn()
}

// This shoud not log since the logger was cleaned up.
// This should not log since the logger was cleaned up.
l.Info(bg, "hello")
assert.Equal(t, "no logs", 0, tb.logs)
}
Expand Down

0 comments on commit e195bb8

Please sign in to comment.