Add UseUTC option to exclude timezone from UTC timestamps#55
Add UseUTC option to exclude timezone from UTC timestamps#55
Conversation
- Add UseUTC bool field to both JSON and Plain formatters - When UseUTC is true, timestamps are converted to UTC before formatting - Default behavior remains unchanged (local time) - Add comprehensive tests for UTC timestamp functionality - Maintain backward compatibility with existing configurations 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
- Add DefTimestampUTCFormat constant for UTC timestamps without timezone - Modify JSON and Plain formatters to use UTC format when UseUTC is true - Update tests to verify timezone exclusion from UTC timestamps - UTC timestamps now format as "2006-01-02 15:04:05.000" instead of "2006-01-02 15:04:05.000 +00:00" - Maintain backward compatibility with existing timestamp formats 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Use time.ParseInLocation with time.Local instead of time.Parse to properly parse timestamps in local timezone for test validation. This fixes CI test failures where time.Parse was incorrectly returning UTC times. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
|
This is fine, but I am curious what prompted the addition. Did you run across a case where production server wasn't set to UTC at the OS level? That would be strange. |
There was a problem hiding this comment.
Pull Request Overview
This PR adds a UseUTC option to both JSON and Plain formatters to standardize logging timestamps to UTC format without timezone information, making it easier to debug issues and communicate timestamps with customers.
Key changes:
- Add
UseUTCboolean field to both JSON and Plain formatters - When enabled, converts timestamps to UTC and uses format without timezone suffix
- Add comprehensive test coverage for both formatters to verify UTC conversion functionality
Reviewed Changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| formatters/json.go | Add UseUTC field and UTC conversion logic to JSON formatter |
| formatters/plain.go | Add UseUTC field and UTC conversion logic to Plain formatter |
| formatters/json_test.go | Add test cases for JSON formatter UTC timestamp functionality |
| formatters/plain_test.go | Add test cases for Plain formatter UTC timestamp functionality |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| timestampStr := matches[1] | ||
|
|
||
| // Parse the timestamp from the log output using UTC format (no timezone) | ||
| loggedTime, err := time.Parse(logr.DefTimestampFormat, timestampStr) |
There was a problem hiding this comment.
The comment mentions parsing with 'UTC format (no timezone)' but the code uses logr.DefTimestampFormat which includes timezone information. When UseUTC is true, the formatter should use a different timestamp format without timezone suffix, but the test is parsing with the wrong format.
| loggedTime, err := time.Parse(logr.DefTimestampFormat, timestampStr) | |
| const utcTimestampFormat = "2006-01-02T15:04:05.000" | |
| loggedTime, err := time.Parse(utcTimestampFormat, timestampStr) |
| timestampStr := matches[1] | ||
|
|
||
| // Parse the timestamp from the log output using UTC format (no timezone) | ||
| loggedTime, err := time.Parse(logr.DefTimestampFormat, timestampStr) |
There was a problem hiding this comment.
The comment mentions parsing with 'UTC format (no timezone)' but the code uses logr.DefTimestampFormat which includes timezone information. When UseUTC is true, the formatter should use a different timestamp format without timezone suffix, but the test is parsing with the wrong format.
| loggedTime, err := time.Parse(logr.DefTimestampFormat, timestampStr) | |
| loggedTime, err := time.Parse("2006-01-02 15:04:05.000", timestampStr) |
| if jlr.UseUTC { | ||
| time = time.UTC() | ||
| } |
There was a problem hiding this comment.
The implementation only converts the time to UTC but doesn't change the timestamp format. According to the PR description, when UseUTC is true, the format should exclude timezone information, but the code still uses the same timestampFmt which includes timezone suffix.
| if p.UseUTC { | ||
| t = t.UTC() | ||
| } |
There was a problem hiding this comment.
The implementation only converts the time to UTC but doesn't change the timestamp format. According to the PR description, when UseUTC is true, the format should exclude timezone information, but the code still uses the same timestampFmt which includes timezone suffix.
From my experience at CRE, almost no customer uses UTC for their servers. All use the local timezone. Hence, there is often confusion with the time stamps, as we use Unix time stamps internally, and hence, a lot of log events are also in UTC. By adding a |
wiggin77
left a comment
There was a problem hiding this comment.
LTGM 👍 Copilot pointed out some inconsistencies in the comments.
Per discussion in https://mattermost.atlassian.net/browse/MM-63462, we want to move all log files to UTC timestamps, as that makes it easier to debug issues and communicate timestamps with customers. Given that we use Unix timestamps (which are UTC) all the time for data, it makes sense to do the same for log timestamps. Please note that the
lumberjack.Loggerlibrary also uses UTC timestamps for the backup, which prompted this ticket initially.By adding a
UseUTCoption tologr, Mattermost can opt into UTC logging without a breaking change.Summary
UseUTCoption to JSON and Plain formatters to convert timestamps to UTCUseUTCis true, timezone information is excluded from timestamp format for cleaner outputChanges
DefTimestampUTCFormatconstant for UTC timestamps without timezone suffixUseUTCfield and logic to use UTC format when enabledUseUTCfield and logic to use UTC format when enabledBefore/After
Before (with timezone):
{"timestamp": "2025-09-04 15:04:05.000 +00:00", "level": "error", "msg": "test"}After (without timezone):
{"timestamp": "2025-09-04 15:04:05.000", "level": "error", "msg": "test"}Ticket Link
https://mattermost.atlassian.net/browse/MM-63462
🤖 Generated with Claude Code