<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/the-better-test-alternative-to-slog-discardhandler-n8xgwluni" -->

---
title: The better test alternative to slog.DiscardHandler
description: Go's testing.T.Output() method, introduced in Go 1.25, is presented as a better alternative to slog.DiscardHandler for silencing logs in tests. Unlike the...
canonical: https://daily.dev/posts/the-better-test-alternative-to-slog-discardhandler-n8xgwluni
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: The better test alternative to slog.DiscardHandler | daily.dev
og:description: Go's testing.T.Output() method, introduced in Go 1.25, is presented as a better alternative to slog.DiscardHandler for silencing logs in tests. Unlike the...
og:url: https://daily.dev/posts/the-better-test-alternative-to-slog-discardhandler-n8xgwluni
og:image: https://api.daily.dev/og/posts/n8XgWlUNi.png
og:image:alt: The better test alternative to slog.DiscardHandler
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# The better test alternative to slog.DiscardHandler

**[Boldly Go](https://daily.dev/sources/boldlygo)** · 2 min read · 5 upvotes · 0 comments

## Summary

Go's testing.T.Output() method, introduced in Go 1.25, is presented as a better alternative to slog.DiscardHandler for silencing logs in tests. Unlike the discard handler, which throws logs away entirely, t.Output() routes log output to the test's own output stream, so nothing is shown when a test passes but log output appears automatically when a test fails, aiding debugging. The technique works with slog, the standard log package, and any third-party logger or writer, not just slog.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://boldlygo.tech/archive/2026-09-17-the-better-test-alternative-to-slog.discardhandler>

## Questions this post answers

### What is the best way to silence slog output in Go tests without losing it when a test fails?

Log to t.Output() instead of using slog.DiscardHandler. Introduced in Go 1.25, testing.T.Output() (also available on testing.B and testing.F) returns an io.Writer tied to the test's output stream; nothing is printed when a test passes, but the log output appears automatically in the failure output when a test fails, unlike a fully discarded handler which loses that information.

_daily.dev surfaces practical Go testing patterns like this for developers refining their logging setup._

### How did people discard slog output in tests before Go 1.24 added slog.DiscardHandler?

Developers manually created a handler writing to io.Discard, for example slog.New(slog.NewTextHandler(io.Discard, nil)). The slog package itself was introduced in Go 1.21 (August 2023) without a built-in discard option, so this workaround was necessary until Go 1.24 added slog.DiscardHandler as a simpler alternative.

_Developers tracking Go's evolving standard library rely on daily.dev to keep up with changes like slog's history._

### Can testing.T.Output() in Go be used for things other than slog logging?

Yes, t.Output() is a general io.Writer tied to the test's output stream, so it works with the standard log package, any third-party logging library, or any other writer unrelated to logging at all. It is not limited to slog-based loggers, making it a flexible tool for capturing diagnostic output only when a test fails.

_daily.dev helps Go developers spot versatile testing techniques like this one as they harden their test suites._

## Similar posts on daily.dev

- [slog.DiscardHandler](https://daily.dev/posts/slog-discardhandler-eogk9yu22) · Boldly Go · 5 upvotes · 1 comments
- [slog.Handler](https://daily.dev/posts/slog-handler-z4ruvm49z) · Boldly Go · 4 upvotes · 0 comments
- [Backward compatibility](https://daily.dev/posts/backward-compatibility-gywqc9jpx) · Boldly Go · 13 upvotes · 2 comments
- [Unit Testing in Go - A Beginner's Guide](https://daily.dev/posts/unit-testing-in-go---a-beginner-s-guide-cqueimyzl) · freeCodeCamp · 19 upvotes · 0 comments
- [Test logging with ReplaceAttr](https://daily.dev/posts/test-logging-with-replaceattr-m6svj81ym) · Boldly Go · 3 upvotes · 0 comments

---

Tags: [#testing](https://daily.dev/tags/testing), [#golang](https://daily.dev/tags/golang), [#logging](https://daily.dev/tags/logging)

[View this post on daily.dev](https://daily.dev/posts/the-better-test-alternative-to-slog-discardhandler-n8xgwluni)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"The better test alternative to slog.DiscardHandler","url":"https://daily.dev/posts/the-better-test-alternative-to-slog-discardhandler-n8xgwluni","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/the-better-test-alternative-to-slog-discardhandler-n8xgwluni"},"datePublished":"2026-09-17T15:19:14.969Z","dateModified":"2026-09-17T15:32:19.788Z","description":"Go's testing.T.Output() method, introduced in Go 1.25, is presented as a better alternative to slog.DiscardHandler for silencing logs in tests. Unlike the...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/a34bbf94f7c8bcbf828174d099f2faa8?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/a34bbf94f7c8bcbf828174d099f2faa8?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Boldly Go","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Boldly Go","logo":"https://media.daily.dev/image/upload/s--0HXlQZhy--/f_auto,q_auto/v1780213594/logos/boldlygo?_a=BAMAMiWQ0","url":"https://daily.dev/sources/boldlygo"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/the-better-test-alternative-to-slog-discardhandler-n8xgwluni","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":5},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"testing,golang,logging","timeRequired":"PT2M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Boldly Go","item":"https://daily.dev/sources/boldlygo"},{"@type":"ListItem","position":3,"name":"The better test alternative to slog.DiscardHandler"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/the-better-test-alternative-to-slog-discardhandler-n8xgwluni#faq","mainEntity":[{"@type":"Question","name":"What is the best way to silence slog output in Go tests without losing it when a test fails?","acceptedAnswer":{"@type":"Answer","text":"Log to t.Output() instead of using slog.DiscardHandler. Introduced in Go 1.25, testing.T.Output() (also available on testing.B and testing.F) returns an io.Writer tied to the test's output stream; nothing is printed when a test passes, but the log output appears automatically in the failure output when a test fails, unlike a fully discarded handler which loses that information. daily.dev surfaces practical Go testing patterns like this for developers refining their logging setup."}},{"@type":"Question","name":"How did people discard slog output in tests before Go 1.24 added slog.DiscardHandler?","acceptedAnswer":{"@type":"Answer","text":"Developers manually created a handler writing to io.Discard, for example slog.New(slog.NewTextHandler(io.Discard, nil)). The slog package itself was introduced in Go 1.21 (August 2023) without a built-in discard option, so this workaround was necessary until Go 1.24 added slog.DiscardHandler as a simpler alternative. Developers tracking Go's evolving standard library rely on daily.dev to keep up with changes like slog's history."}},{"@type":"Question","name":"Can testing.T.Output() in Go be used for things other than slog logging?","acceptedAnswer":{"@type":"Answer","text":"Yes, t.Output() is a general io.Writer tied to the test's output stream, so it works with the standard log package, any third-party logging library, or any other writer unrelated to logging at all. It is not limited to slog-based loggers, making it a flexible tool for capturing diagnostic output only when a test fails. daily.dev helps Go developers spot versatile testing techniques like this one as they harden their test suites."}}]}
```

