<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/expanding-errors-with-replaceattr-vfxd8vujv" -->

---
title: Expanding errors with ReplaceAttr | daily.dev
description: A technique for expanding Go's slog.ReplaceAttr to include stack trace details from errors, specifically those wrapped with github.com/pkg/errors' WithStack....
canonical: https://daily.dev/posts/expanding-errors-with-replaceattr-vfxd8vujv
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Expanding errors with ReplaceAttr | daily.dev
og:description: A technique for expanding Go's slog.ReplaceAttr to include stack trace details from errors, specifically those wrapped with github.com/pkg/errors' WithStack....
og:url: https://daily.dev/posts/expanding-errors-with-replaceattr-vfxd8vujv
og:image: https://api.daily.dev/og/posts/VfxD8vujV.png
og:image:alt: Expanding errors with ReplaceAttr
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.

# Expanding errors with ReplaceAttr

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

## Summary

A technique for expanding Go's slog.ReplaceAttr to include stack trace details from errors, specifically those wrapped with github.com/pkg/errors' WithStack. The approach checks for a stackTracer interface, then replaces the simple error attribute with a slog.Group containing the message and stack trace. Covers a common pitfall (passing err.Error() as a string loses stack info) and shows ways to render the stack trace more cleanly, as an array of frames or fully custom formatted strings for text and JSON output. Ends with a promo for the author's Boot.Dev logging course.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://boldlygo.tech/archive/2026-09-30-expanding-errors-with-replaceattr>

## Questions this post answers

### Why doesn't my Go slog stack trace show up when I log err.Error() as a string?

Once an error is converted to a string via err.Error(), all additional information like a stack trace is lost, so a ReplaceAttr function cannot extract it. To preserve stack trace data, pass the raw error value or use slog.Any with the error itself, such as logger.Error("failure!", "error", err), rather than converting it to a string first.

_Developers debugging missing stack traces in Go logs can compare approaches like this on daily.dev._

### How can I include a stack trace from github.com/pkg/errors in Go structured logs using slog?

Define a stackTracer interface requiring StackTrace() and use errors.AsType to check if a wrapped error implements it, then use a slog ReplaceAttr function to replace the error attribute with an slog.Group containing both the message and the stack trace. The stack can be converted to []pkgerr.Frame or formatted into a custom string slice for cleaner JSON output.

_Go developers building structured logging pipelines can find implementation patterns like this on daily.dev._

## Community discussion

Top comments from developers on daily.dev.

**@bob72** · 0 upvotes

> TL;DR: sloppy article with AD.

## Similar posts on daily.dev

- [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.Handler.WithAttrs](https://daily.dev/posts/slog-handler-withattrs-l2fs9cp1u) · Boldly Go · 29 upvotes · 1 comments
- [How to Handle Errors in Go](https://daily.dev/posts/how-to-handle-errors-in-go-tlpqkl2og) · JetBrains · 25 upvotes · 1 comments
- [Go proposal: Type-safe error checking](https://daily.dev/posts/go-proposal-type-safe-error-checking-u4b58mpwl) · Anton Zhiyanov · 70 upvotes · 5 comments

---

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

[View this post on daily.dev](https://daily.dev/posts/expanding-errors-with-replaceattr-vfxd8vujv)

```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":"Expanding errors with ReplaceAttr","url":"https://daily.dev/posts/expanding-errors-with-replaceattr-vfxd8vujv","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/expanding-errors-with-replaceattr-vfxd8vujv"},"datePublished":"2026-09-30T21:11:13.416Z","dateModified":"2026-09-30T21:11:32.794Z","description":"A technique for expanding Go's slog.ReplaceAttr to include stack trace details from errors, specifically those wrapped with github.com/pkg/errors' WithStack....","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/expanding-errors-with-replaceattr-vfxd8vujv","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":10},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"keywords":"golang","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":"Expanding errors with ReplaceAttr"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/expanding-errors-with-replaceattr-vfxd8vujv","comment":[{"@type":"Comment","text":"TL;DR: sloppy article with AD.","datePublished":"2026-10-01T19:51:17.529Z","url":"https://daily.dev/posts/VfxD8vujV#c-ZO0GVqwPS","author":{"@type":"Person","name":"Bob","url":"https://daily.dev/bob72","image":"https://avatars.githubusercontent.com/u/193962566?v=4"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/expanding-errors-with-replaceattr-vfxd8vujv#faq","mainEntity":[{"@type":"Question","name":"Why doesn't my Go slog stack trace show up when I log err.Error() as a string?","acceptedAnswer":{"@type":"Answer","text":"Once an error is converted to a string via err.Error(), all additional information like a stack trace is lost, so a ReplaceAttr function cannot extract it. To preserve stack trace data, pass the raw error value or use slog.Any with the error itself, such as logger.Error(\"failure!\", \"error\", err), rather than converting it to a string first. Developers debugging missing stack traces in Go logs can compare approaches like this on daily.dev."}},{"@type":"Question","name":"How can I include a stack trace from github.com/pkg/errors in Go structured logs using slog?","acceptedAnswer":{"@type":"Answer","text":"Define a stackTracer interface requiring StackTrace() and use errors.AsType to check if a wrapped error implements it, then use a slog ReplaceAttr function to replace the error attribute with an slog.Group containing both the message and the stack trace. The stack can be converted to []pkgerr.Frame or formatted into a custom string slice for cleaner JSON output. Go developers building structured logging pipelines can find implementation patterns like this on daily.dev."}}]}
```

