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

---
title: slog.Handler.WithAttrs | daily.dev
description: An explanation of why slog.Handler's WithAttrs method returns a new handler rather than mutating in place. Two reasons are given: it makes reusing a logger...
canonical: https://daily.dev/posts/slog-handler-withattrs-l2fs9cp1u
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: slog.Handler.WithAttrs | daily.dev
og:description: An explanation of why slog.Handler's WithAttrs method returns a new handler rather than mutating in place. Two reasons are given: it makes reusing a logger...
og:url: https://daily.dev/posts/slog-handler-withattrs-l2fs9cp1u
og:image: https://api.daily.dev/og/posts/L2fS9Cp1U.png
og:image:alt: slog.Handler.WithAttrs
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.Handler.WithAttrs

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

## Summary

An explanation of why slog.Handler's WithAttrs method returns a new handler rather than mutating in place. Two reasons are given: it makes reusing a logger across goroutines safe from race conditions and field accumulation, and it lets each handler choose an efficient internal representation. The standard library's text and JSON handlers pre-format attributes when WithAttrs is called, caching the formatted string so it isn't re-formatted on every log call, with a small tradeoff being that formatting happens even if the log line is never emitted.

## Full article

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

## Questions this post answers

### Why does slog.Handler.WithAttrs return a new handler instead of modifying the existing one in place?

Returning a new handler keeps logger reuse safe and efficient. Because WithAttrs is not mutating, calling logger.With inside a loop or goroutine cannot introduce races or cause unwanted fields to accumulate across concurrent calls. It also lets each handler choose its own internal representation and apply optimizations, such as pre-formatting attributes once instead of repeatedly.

_daily.dev surfaces Go internals like this for developers designing safe, concurrent logging setups._

### Why do the standard library's slog text and JSON handlers format attributes when WithAttrs is called instead of when a log entry is emitted?

They format eagerly to avoid redundant work: since WithAttrs returns a new handler carrying fixed attributes, the handler formats those attributes once and caches the formatted representation, rather than re-formatting the same key-value pair on every subsequent log call. The tradeoff is that formatting occurs even if a log line built from that handler is ultimately never emitted.

_Understanding slog's caching tradeoffs helps developers profiling Go logging performance, a topic daily.dev tracks._

## Community discussion

Top comments from developers on daily.dev.

**@agustinbarrientos** · 0 upvotes

> I think `WithAttrs` behaves more like partial application than a mutable builder because the formatting cost is paid before any record uses it

## Similar posts on daily.dev

- [slog.Handler.WithGroup](https://daily.dev/posts/slog-handler-withgroup-uccfkac85) · Boldly Go · 4 upvotes · 0 comments
- [slog.Group](https://daily.dev/posts/slog-group-jgispqf2d) · Boldly Go · 7 upvotes · 1 comments
- [Test logging with ReplaceAttr](https://daily.dev/posts/test-logging-with-replaceattr-m6svj81ym) · Boldly Go · 3 upvotes · 0 comments
- [slog.GroupAttrs](https://daily.dev/posts/slog-groupattrs-cmhm9xwg4) · Boldly Go · 16 upvotes · 2 comments
- [slog.Group](https://daily.dev/posts/slog-group-olh3jgzix) · Boldly Go · 10 upvotes · 1 comments

---

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

[View this post on daily.dev](https://daily.dev/posts/slog-handler-withattrs-l2fs9cp1u)

```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.Handler.WithAttrs","url":"https://daily.dev/posts/slog-handler-withattrs-l2fs9cp1u","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/slog-handler-withattrs-l2fs9cp1u"},"datePublished":"2026-09-03T19:43:25.356Z","dateModified":"2026-09-14T09:11:55.407Z","description":"An explanation of why slog.Handler's WithAttrs method returns a new handler rather than mutating in place. Two reasons are given: it makes reusing a logger...","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-handler-withattrs-l2fs9cp1u","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":29},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"keywords":"golang,logging","timeRequired":"PT3M"}
{"@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.Handler.WithAttrs"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/slog-handler-withattrs-l2fs9cp1u","comment":[{"@type":"Comment","text":"I think WithAttrs behaves more like partial application than a mutable builder because the formatting cost is paid before any record uses it","datePublished":"2026-09-05T17:47:58.048Z","url":"https://daily.dev/posts/L2fS9Cp1U#c-FCSJyrEZf","author":{"@type":"Person","name":"Agustin Barrientos","url":"https://daily.dev/agustinbarrientos","image":"https://media.daily.dev/image/upload/s--5ayxQnqn--/f_auto/v1788281802/avatars/avatar_wQYYVe5Tbj0NJ7C7qPoa8?_a=BAMAMicg0"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/slog-handler-withattrs-l2fs9cp1u#faq","mainEntity":[{"@type":"Question","name":"Why does slog.Handler.WithAttrs return a new handler instead of modifying the existing one in place?","acceptedAnswer":{"@type":"Answer","text":"Returning a new handler keeps logger reuse safe and efficient. Because WithAttrs is not mutating, calling logger.With inside a loop or goroutine cannot introduce races or cause unwanted fields to accumulate across concurrent calls. It also lets each handler choose its own internal representation and apply optimizations, such as pre-formatting attributes once instead of repeatedly. daily.dev surfaces Go internals like this for developers designing safe, concurrent logging setups."}},{"@type":"Question","name":"Why do the standard library's slog text and JSON handlers format attributes when WithAttrs is called instead of when a log entry is emitted?","acceptedAnswer":{"@type":"Answer","text":"They format eagerly to avoid redundant work: since WithAttrs returns a new handler carrying fixed attributes, the handler formats those attributes once and caches the formatted representation, rather than re-formatting the same key-value pair on every subsequent log call. The tradeoff is that formatting occurs even if a log line built from that handler is ultimately never emitted. Understanding slog's caching tradeoffs helps developers profiling Go logging performance, a topic daily.dev tracks."}}]}
```

