Skip to content

feat(logger): preserve slog argument order and add SortArguments option - #808

Merged
MarvinJWendt merged 1 commit into
pterm:masterfrom
ChrisJr404:feat/logger-sort-arguments
Oct 10, 2026
Merged

MarvinJWendt merged 1 commit into
pterm:masterfrom
ChrisJr404:feat/logger-sort-arguments

Conversation

@ChrisJr404

Copy link
Copy Markdown
Contributor

Fixes #611

What

The SlogHandler collected slog attrs into a map[string]any before 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:

  • Rewrites SlogHandler.Handle to build the arguments in the order they were supplied: attrs bound via WithAttrs first, then the record's own attrs, each in insertion order. This matches the behavior of slog's own TextHandler, so output is now stable and predictable. (json.Marshal already sorted the JSON formatter's keys, so this only affects text output.)
  • Adds an opt-in SortArguments option on the Logger (with the usual WithSortArguments(...) 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 in combineArgs, so it works consistently for both the variadic Args(...) 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; gofmt and go vet are clean. Public signatures are unchanged (a field and a With* method are added), and ArgsFromMap is left untouched.

Happy to add a small _examples/logger entry for the new option if you'd like it in the generated docs.

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

codecov Bot commented Aug 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.09%. Comparing base (bacb2fc) to head (d8b815b).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ChrisJr404

Copy link
Copy Markdown
Contributor Author

Heads up on the red windows-latest check: the failures are all in area_printer_test.go (the TestAreaPrinter_* cases), which assert ANSI sequences like \x1b[2K that pterm doesn't emit on Windows. They look pre-existing and unrelated to this change:

  • This PR only touches logger.go, logger_test.go, and slog_handler.go — it doesn't modify area_printer.go or area_printer_test.go.
  • The same 8 tests fail the same way on master's own windows-latest run ("" does not contain "\x1b[2K"); the last green Windows build there was back on 2026-07-05, right before that test suite landed.
  • The logger/slog tests added here pass on ubuntu-latest and macos-latest, and none of them are among the Windows failures.

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.

@ChrisJr404

Copy link
Copy Markdown
Contributor Author

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.

@MarvinJWendt
MarvinJWendt merged commit 0aebb19 into pterm:master Oct 10, 2026
6 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keep order of arguments in SlogHandler

2 participants