Skip to content

Commit c19febc

Browse files
committed
add millisecond precision
1 parent 4813b70 commit c19febc

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

‎Sources/AWSLambdaRuntime/Logging/JSONLogHandler.swift‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import Logging
1717

1818
#if canImport(Darwin)
19-
import Darwin.C
19+
import Darwin
2020
#elseif canImport(Glibc)
2121
import Glibc
2222
#elseif canImport(Musl)
@@ -169,7 +169,10 @@ public struct JSONLogHandler: LogHandler {
169169
/// Returns nil if encoding fails.
170170
internal static func encodeLogEntry(_ logEntry: LogEntry) -> Data? {
171171
let encoder = JSONEncoder()
172-
encoder.dateEncodingStrategy = .iso8601
172+
encoder.dateEncodingStrategy = .custom { date, encoder in
173+
var container = encoder.singleValueContainer()
174+
try container.encode(date.formatted(Date.ISO8601FormatStyle(includingFractionalSeconds: true)))
175+
}
173176
encoder.outputFormatting = [] // Compact output (no pretty printing)
174177
return try? encoder.encode(logEntry)
175178
}

‎Tests/AWSLambdaRuntimeTests/JSONLogHandlerTests.swift‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ struct JSONLogHandlerTests {
183183
let timestamp = entry?.timestamp
184184
#expect(timestamp != nil)
185185

186-
// Verify it matches ISO 8601 format (e.g. "2024-01-16T10:30:45Z")
187-
let iso8601Pattern = #"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\\d+)?Z$"#
186+
// Verify it matches ISO 8601 format with milliseconds (e.g. "2024-01-16T10:30:45.123Z")
187+
let iso8601Pattern = #"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{1,6}Z$"#
188188
let matches = timestamp?.range(of: iso8601Pattern, options: .regularExpression) != nil
189-
#expect(matches, "Timestamp '\(timestamp ?? "")' should be in ISO 8601 format")
189+
#expect(matches, "Timestamp '\(timestamp ?? "")' should be in ISO 8601 format with fractional seconds")
190190
}
191191

192192
// MARK: - Metadata subscript

0 commit comments

Comments
 (0)