Repository navigation
feat(logger): preserve slog argument order and add SortArguments option - #808
Conversation
The SlogHandler collected attrs into a map before handing them to the logger, so the text formatter printed key-value pairs in Go's randomized map iteration order. Build the arguments in the order they were supplied instead (WithAttrs attrs first, then the record's own attrs, matching slog's own text handler), so output is stable and predictable. For users who prefer alphabetical output, add a SortArguments option (WithSortArguments) that sorts the arguments by key. It is off by default, so the original order is kept unless explicitly enabled. Fixes pterm#611
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #808 +/- ##
==========================================
- Coverage 92.21% 92.09% -0.13%
==========================================
Files 66 66
Lines 4318 4324 +6
==========================================
Hits 3982 3982
- Misses 244 249 +5
- Partials 92 93 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Heads up on the red
Happy to help if there's interest in gating those area-printer assertions on Windows, but that felt out of scope for a logger change so I left it alone. Let me know if you'd like anything adjusted here. |
|
Heads up on the red windows-latest check: the failures are all in the TestAreaPrinter_* suite, and they already fail on master. The same 8 tests fail on the base commit (bacb2fc) on windows, so the red check isn't coming from this branch. This PR only touches the slog handler and logger, and its logger tests pass on windows. Fine to re-run, but the area printer tests probably need a separate fix on master. |
Fixes #611
What
The
SlogHandlercollected slog attrs into amap[string]anybefore handing them to the logger. Because Go randomizes map iteration order, the colorful (text) formatter printed the key-value pairs in a different order on every call. This makes structured logs hard to scan and diff.This PR:
SlogHandler.Handleto build the arguments in the order they were supplied: attrs bound viaWithAttrsfirst, then the record's own attrs, each in insertion order. This matches the behavior of slog's ownTextHandler, so output is now stable and predictable. (json.Marshalalready sorted the JSON formatter's keys, so this only affects text output.)SortArgumentsoption on theLogger(with the usualWithSortArguments(...)builder) for users who prefer alphabetical output. It is off by default, so the original order is kept unless explicitly enabled. The sort is applied incombineArgs, so it works consistently for both the variadicArgs(...)path and the slog path.This follows the approach agreed on in #611 (option 3: keep original order by default, offer an alphabetical-sort toggle).
Tests
TestSlogHandlerPreservesArgumentOrder— non-alphabetical keys stay in supplied order (WithAttrs then record attrs).TestSlogHandlerSortArgumentsSortsAlphabetically—WithSortArguments()produces alphabetical order.TestLoggerWithSortArguments— the builder sets the field and does not mutate the receiver.go test -race ./...passes across the module;gofmtandgo vetare clean. Public signatures are unchanged (a field and aWith*method are added), andArgsFromMapis left untouched.Happy to add a small
_examples/loggerentry for the new option if you'd like it in the generated docs.