Created
November 1, 2023 22:38
-
-
Save jeffmccune/bde347a3f070f57256ee9c037544f436 to your computer and use it in GitHub Desktop.
log source in tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- FAIL: TestServer (0.00s) | |
--- FAIL: TestServer/NewServer (0.00s) | |
testutils.go:16: DBG identity.go:80: could not get oidc provider err="Get \"/.well-known/openid-configuration\": unsupported protocol scheme \"\"" | |
server_suite_test.go:28: | |
Error Trace: /home/jeff/workspace/holos-run/holos-server-go/internal/server/server_suite_test.go:28 | |
Error: Received unexpected error: | |
Get "/.well-known/openid-configuration": unsupported protocol scheme "" | |
Test: TestServer/NewServer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package testutils | |
import ( | |
"fmt" | |
"github.com/lmittmann/tint" | |
"log/slog" | |
"path/filepath" | |
"testing" | |
) | |
type testWriter struct { | |
t testing.TB | |
} | |
func (w *testWriter) Write(b []byte) (int, error) { | |
w.t.Logf("%s", b) | |
return len(b), nil | |
} | |
// TestLogger is an adapter that sends output to t.Log so that log messages are | |
// associated with failing tests. | |
func TestLogger(t testing.TB) *slog.Logger { | |
t.Helper() | |
testHandler := tint.NewHandler(&testWriter{t}, &tint.Options{ | |
Level: slog.LevelDebug, | |
AddSource: true, | |
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { | |
if len(groups) == 0 { | |
switch key := a.Key; key { | |
case slog.TimeKey: | |
return slog.Attr{} // Remove the timestamp | |
case slog.SourceKey: | |
if src, ok := a.Value.Any().(*slog.Source); ok { | |
name := fmt.Sprintf("%s:%d:", filepath.Base(src.File), src.Line) | |
return slog.String("source", name) | |
} | |
} | |
} | |
return a | |
}, | |
}) | |
return slog.New(testHandler) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment