Sitemap

Logging in go-api-basic

6 min readJul 13, 2021

--

A bearded gopher with a lightsaber cutting up some logs
Press enter or click to view image in full size

Press enter or click to view image in full size
Press enter or click to view image in full size
The above error log demonstrates a log for an error with stack trace turned off.
Press enter or click to view image in full size
Press enter or click to view image in full size

Peter Bourgon recently tweeted the above about github.com/pkg/errors. He's not wrong. In general the standard library is always the way to go. For years I only used the standard library for raising errors and would annotate each error with the function name in order to capture a pseudo stack trace exactly like Rob Pike does in this post (the errs.Op field). I believe in annotating and logging every error - not everyone does. It can cause noise in your logs, but in my experience, it's helpful. After years of trying to annotate every error though, I found I made too many mistakes. I kept forgetting to log the function name or I would misspell it, etc. I switched to use github.com/pkg/errors because of it's stack trace capability as well as the integration zerolog has with it. Stack trace for errors can certainly be overkill though, hence the ability to turn it on/off with a service for tactical usage can come in handy.

--

--

Dan Gillis
Dan Gillis

Written by Dan Gillis

Go enthusiast; Loyalty/CRM Technology Leader; Drummer; Vinyl geek; Husband/Dad