Writing a Slog Handler Part 4: Final Thoughts

Wrapping up the slog series.

By Will Roden
July, 20 2023 | Reading time: 2 minutes
This article is part of a series

I learned quite a bit while writing this series and the slog handlers. My biggest takeaway is that there is a lot of subtle complexity in that simple Handler interface. The wrapper came together pretty quickly and gave me a false sense of confidence. Then all that subtle complexity showed up when I started writing the human-readable handler. I'm glad for the experience.

Along the way I found a concurrency issue in the standard library's handlers and got to make my first code contribution to go. I'm proud of the "Contributor" label next to my name on the issue.

In "log/slog", all three public handlers are backed by a private handler called commonHandler. I got a good look at it when I worked on the concurrency issue. It turns out that it uses both the approaches I tried in my handler. Normally it uses the Attr collector approach, but when it is called from JSONHandler it uses the incremental builder approach.

In my opinion, combining all three handlers into one makes it hard to follow with the benefit of being a bit DRYer. I think this a missed opportunity. If the implementations had been kept separate and simpler, "log/slog" could have been a good example of how to write a slog handler. It still serves as a good example, but it is left to the reader to work out what is going on, what handlerState is there for and what parts of it are used by which handlers.

It's a little late in the 1.21 release cycle, but I think there is room to split them out in a future release without breaking backward compatibility. Whether that's a wise use of dev time is another matter.

Overall, I think slog is going to be a great addition to the standard library. Zap, Logrus, Zerolog and the others did a good job filling the gap, but it's a relief to finally have a standard library solution.

I can't believe I made it through four posts about slog without invoking Donald Rumsfeld's infamous, "long, hard slog" quote.

Remember to check out the final result at github.com/WillAbides/actionslog.

This article is part of a series