Discard Slog Logs In Tests

I want to have logs emitted when I run my app, but I don't want those same logs emitted when I run go test because they clutter up the output.

Solution

Set this up in your test package:


func init() {
    // Set this handler as the default for slog
    slog.SetDefault(slog.New(slog.NewTextHandler(io.Discard, nil)))
}
        

This solution was inspired by a long running github issue to solve this same issue.