Sitemap

Traceerr: Simplifying Error Handling and Debugging in Go

3 min readJan 13, 2025
Press enter or click to view image in full size

As developers, debugging and error handling can often feel like a tedious part of our workflow. When something goes wrong, we need clarity: What happened? Where did it happen? What led to it? In Go, while the standard library provides great tools for errors, capturing context and logs leading to the error can be a challenge. This is where Traceerr, a lightweight Go library, steps in.

Why Traceerr?

Errors often don’t tell the full story. They’re snapshots of a problem, not the journey. With Traceerr, every error becomes a complete log of events, enriched with:

  • Automatic stack traces: Traceerr captures the function calls that led to the error.
  • Pre-error logs: Logs leading up to the error are included in the trace for context.
  • Concise logging methods: Logging functions like Print, Printf, and Errorln follow Go’s idioms, making it easy to adopt.

All this, without changing your current workflow.

Core Features

1- Stack Traces on Errors
Every error created with traceerr.Error or traceerr.Errorf comes with a detailed stack trace.

2- Pre-Error Logs
Traceerr stores the last N logs (configurable) and includes them in the error report.

3- Customizable Logging

Use environment variables like LOG_PERSIST_TRACE to set how many logs to store.

Enable real-time logging with DEBUG=1.

4- Minimal Overhead

Simple to integrate.

Leverages Go idioms for logging and error handling.

How It Works

Traceerr introduces a Logger with two key components:

  • A ring buffer that stores the last N logs.
  • Error-handling methods (Error, Errorf, etc.) that combine stack traces and logs into a single output.
Press enter or click to view image in full size

Expected Output

01-12-2025 21:19:26 - Starting funcB
01-12-2025 21:19:26 - Starting funcA
01-12-2025 21:19:26 - Processing step 1 in funcA
---- LOGS ----
01-12-2025 21:19:26 - Starting funcB
01-12-2025 21:19:26 - Starting funcA
01-12-2025 21:19:26 - Processing step 1 in funcA
---- ERROR ----
Program completed with error: funcB encountered an error: Something went wrong in funcA
Stack trace:
main.funcA
/path/to/main.go:10
main.funcB
/path/to/main.go:16
main.main
/path/to/main.go:22ba

Why Choose Traceerr?

  • Developer-Friendly: Traceerr integrates seamlessly into existing Go projects.
  • Improves Debugging: Provides all the context you need — logs and stack traces — without extra effort.
  • Highly Configurable: Fine-tune log persistence and debugging output to suit your workflow.

Get Started Today

Install Traceerr in your Go project:

go get github.com/devalexandre/traceerr

Set up your environment variables:

export LOG_PERSIST_TRACE=50
export DEBUG=1

Conclusion

Traceerr bridges the gap between simple error messages and full-context debugging. Whether you’re working on small projects or large-scale systems, this library ensures you’re never left in the dark when things go wrong.

Explore the Traceerr GitHub repository and try it out in your next project. Your future debugging self will thank you. 🚀

--

--

Alexandre E Souza
Alexandre E Souza

Written by Alexandre E Souza

Microservice Evangelist | Go Lover | JS Lover

No responses yet