Skip to content

slog-handler-guide: make time regex timezone-aware - #75

Open
cu-min wants to merge 1 commit into
golang:masterfrom
cu-min:fix-slog-handler-timezone
Open

slog-handler-guide: make time regex timezone-aware#75
cu-min wants to merge 1 commit into
golang:masterfrom
cu-min:fix-slog-handler-timezone

Conversation

@cu-min

@cu-min cu-min commented Aug 12, 2026

Copy link
Copy Markdown

The indenthandler{1..4} tests assert the log timestamp with the regex time: [-0-9T:.]+Z?, which only accepts a UTC Z suffix. On hosts with a non-UTC timezone (e.g. Asia/Shanghai, UTC+8), log/slog emits a numeric offset and the tests fail:

--- FAIL: Test (0.00s)
    indent_handler_test.go:32:
        got:
        "time: 2026-08-12T16:00:00.858525+08:00\nlevel: INFO\nsource: \".../indent_handler_test.go:16\"\nmsg: \"hello\"\na: 1\nb: true\n..."
        want:
        "time: [-0-9T:.]+Z?\nlevel: INFO\n..."

The + sign and the +08:00 offset are not covered by the [-0-9T:.] character class. This change extends the regex to also accept a numeric UTC offset:

time: [-0-9T:.]+(Z|[+-][0-9]{2}:[0-9]{2})?

Verified on a UTC+8 host: go test ./slog-handler-guide/... passes for all four handlers (previously indenthandler1 and indenthandler2 failed).

The indenthandler tests assert the log timestamp with 'time: [-0-9T:.]+Z?',
which only accepts a UTC 'Z' suffix. On non-UTC hosts log/slog emits a
numeric offset (e.g. '2026-08-12T16:00:00.858525+08:00') and the tests fail
because '+' and the offset are not matched.

Extend the regex to also accept a numeric UTC offset:
    time: [-0-9T:.]+(Z|[+-][0-9]{2}:[0-9]{2})?
@google-cla

google-cla Bot commented Aug 12, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant