Create log timestamps in UTC to avoid local-time races

Use getTime().utc in both message constructors, document the timezone change, and cover concurrent construction and structured formatting. Restore ordinary logging calls in the parallel writer fixture.

Validation: 32 unit tests and 29 regression tests pass. Timestamp sanitizer checks pass under UTC and non-UTC timezones. The full suite retains the known allocator reports in factory-state and allocator.
This commit is contained in:
jdb committed 2026-09-29 09:03:31 -05:00
1 parent 011780e6ab
commit 46245b3f1e
6 files changed
+48 -17

No files matched your search

+21 -1
View File
@@ -1,7 +1,27 @@
import std/[os, osproc, strutils, tempfiles, unittest]
import std/[json, os, osproc, strutils, tempfiles, times, unittest]
import ../src/namespaced_logging
import ./ownership
suite "Log message timestamps":
test "both constructors capture the current instant in UTC":
let before = getTime()
let textMessage = initLogMessage("timestamp", lvlInfo, "text")
let jsonMessage = initLogMessage("timestamp", lvlInfo, %*{"msg": "json"})
let after = getTime()
for message in [textMessage, jsonMessage]:
check message.ts.timezone.name == "Etc/UTC"
check message.ts.utcOffset == 0
check not message.ts.isDst
check message.ts.toTime >= before
check message.ts.toTime <= after
test "structured output represents generated timestamps in UTC":
for message in [initLogMessage("timestamp", lvlInfo, "text"),
initLogMessage("timestamp", lvlInfo, %*{"msg": "json"})]:
let encoded = parseJson(formatJsonStructuredLog(message))["ts"].getStr
check encoded == message.ts.format("yyyy-MM-dd'T'HH:mm:sszzz")
check encoded.endsWith("Z")
type
CloneControl = object
failNext: bool