<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/slog-discardhandler-eogk9yu22" -->

---
title: slog.DiscardHandler | daily.dev
description: DiscardHandler, a slog.Handler implementation in Go's standard library, discards all log output and always reports Enabled as false regardless of level. It's...
canonical: https://daily.dev/posts/slog-discardhandler-eogk9yu22
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: slog.DiscardHandler | daily.dev
og:description: DiscardHandler, a slog.Handler implementation in Go's standard library, discards all log output and always reports Enabled as false regardless of level. It's...
og:url: https://daily.dev/posts/slog-discardhandler-eogk9yu22
og:image: https://api.daily.dev/og/posts/eOgK9yU22.png
og:image:alt: 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.

# slog.DiscardHandler

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

## Summary

DiscardHandler, a slog.Handler implementation in Go's standard library, discards all log output and always reports Enabled as false regardless of level. It's useful for suppressing logs during tests (passing slog.DiscardHandler into slog.New) and for disabling logging based on configuration or runtime environment variables, avoiding the need to write a custom no-op handler.

## Full article

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

## Questions this post answers

### How do I disable logging output in a Go test that uses slog?

Create a logger with slog.New(slog.DiscardHandler) and pass it into the function under test. DiscardHandler discards all log output and its Enabled method returns false for every level, so no log records are processed or written, making it a simple way to silence logging during unit tests.

_Developers wiring up slog in Go tests often compare handler patterns like this on daily.dev._

### How can I conditionally disable logging in a Go application based on an environment variable?

Check the environment variable and assign slog.DiscardHandler to the handler variable when logging should be off, otherwise use a real handler like slog.NewJSONHandler(os.Stderr, nil). Then build the logger with slog.New(handler). DiscardHandler discards all output and reports Enabled as false for all levels, avoiding the overhead of a custom no-op implementation.

_Go developers configuring runtime logging behavior track patterns like this on daily.dev._

## Community discussion

Top comments from developers on daily.dev.

**@nark3d** · 0 upvotes

> If a handler reports Enabled as false for every level, does anything downstream still get told the log was attempted, or does the call vanish entirely?  That distinction matters when you're trying to prove a code path ran without letting its output through.  Discarding proves nothing about whether the call was ever made.  A test that leans on it passes just as happily when somebody deletes the logging line, so it confirms the absence of output rather than the presence of the call.  Would a test that relies on DiscardHandler ever fail if the logging call was deleted outright?

## Similar posts on daily.dev

- [The better test alternative to slog.DiscardHandler](https://daily.dev/posts/the-better-test-alternative-to-slog-discardhandler-n8xgwluni) · Boldly Go · 5 upvotes · 0 comments
- [slog.Handler](https://daily.dev/posts/slog-handler-z4ruvm49z) · Boldly Go · 4 upvotes · 0 comments
- [slog Handler methods](https://daily.dev/posts/slog-handler-methods-rd0bqycf1) · Boldly Go · 10 upvotes · 0 comments
- [Handler level](https://daily.dev/posts/handler-level-vt77hf1pi) · Boldly Go · 12 upvotes · 0 comments
- [slog.Handler.Handle\(\)](https://daily.dev/posts/slog-handler-handle--illw6gctv) · Boldly Go · 0 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/slog-discardhandler-eogk9yu22)

```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":"slog.DiscardHandler","url":"https://daily.dev/posts/slog-discardhandler-eogk9yu22","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/slog-discardhandler-eogk9yu22"},"datePublished":"2026-09-14T17:57:01.010Z","dateModified":"2026-09-14T18:02:55.637Z","description":"DiscardHandler, a slog.Handler implementation in Go's standard library, discards all log output and always reports Enabled as false regardless of level. It's...","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":1,"discussionUrl":"https://daily.dev/posts/slog-discardhandler-eogk9yu22","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":5},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"keywords":"testing,golang,logging","timeRequired":"PT1M"}
{"@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":"slog.DiscardHandler"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/slog-discardhandler-eogk9yu22","comment":[{"@type":"Comment","text":"If a handler reports Enabled as false for every level, does anything downstream still get told the log was attempted, or does the call vanish entirely?  That distinction matters when you’re trying to prove a code path ran without letting its output through.  Discarding proves nothing about whether the call was ever made.  A test that leans on it passes just as happily when somebody deletes the logging line, so it confirms the absence of output rather than the presence of the call.  Would a test that relies on DiscardHandler ever fail if the logging call was deleted outright?","datePublished":"2026-09-26T10:34:14.066Z","url":"https://daily.dev/posts/eOgK9yU22#c-C0T2RBAfh","author":{"@type":"Person","name":"Adam","url":"https://daily.dev/nark3d","image":"https://avatars.githubusercontent.com/u/2162621?v=4"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/slog-discardhandler-eogk9yu22#faq","mainEntity":[{"@type":"Question","name":"How do I disable logging output in a Go test that uses slog?","acceptedAnswer":{"@type":"Answer","text":"Create a logger with slog.New(slog.DiscardHandler) and pass it into the function under test. DiscardHandler discards all log output and its Enabled method returns false for every level, so no log records are processed or written, making it a simple way to silence logging during unit tests. Developers wiring up slog in Go tests often compare handler patterns like this on daily.dev."}},{"@type":"Question","name":"How can I conditionally disable logging in a Go application based on an environment variable?","acceptedAnswer":{"@type":"Answer","text":"Check the environment variable and assign slog.DiscardHandler to the handler variable when logging should be off, otherwise use a real handler like slog.NewJSONHandler(os.Stderr, nil). Then build the logger with slog.New(handler). DiscardHandler discards all output and reports Enabled as false for all levels, avoiding the overhead of a custom no-op implementation. Go developers configuring runtime logging behavior track patterns like this on daily.dev."}}]}
```

