mirror of
https://github.com/apple/swift-log
synced 2026-11-22 03:40:39 +00:00
Route the SwiftLog 1.0 default log method to log(event:) (#503)
### Motivation: The two deprecated `LogHandler` defaults forward to each other: the source-carrying `log(level:message:metadata:source:file:function:line:)` default calls `log(event:)`, whose default calls the source-carrying method, and the SwiftLog 1.0 `log(level:message:metadata:file:function:line:)` default calls the source-carrying method. A handler that implements only `log(event:)`, as the protocol documentation asks, therefore recurses until the stack overflows whenever anything calls the 1.0 method on it. Handlers written against SwiftLog 1.0 that wrap another handler and forward through the 1.0 method reach this path. Differs from #248, where the handler implemented no log method at all. ### Modifications: The 1.0 default builds a `LogEvent` and calls `log(event:)` directly. The source is still the module derived from `file`, as before. Two tests in `CompatibilityTest`: the 1.0 method reaching an event-only handler (SIGSEGV at `main` before the change) and reaching a source-only handler. ### Result: A `LogHandler` implementing only `log(event:)` can be called through every deprecated entry point. No source-breaking change; both deprecated defaults remain. Verified on Linux with Swift 6.1.2: `swift test -Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable` (166 tests) and `swift-format lint --strict` on the changed files. Found by an automated audit loop (Claude); patch and description reviewed by me. --------- Co-authored-by: Vladimir Kukushkin <kukushechkin@apple.com>
This commit is contained in:
1 parent
c5ece24786
commit
53d00853e6
2 files changed
+86
-7
No files matched your search
@@ -280,6 +280,10 @@ extension LogHandler {
|
||||
}
|
||||
|
||||
/// A default implementation for the SwiftLog 1.0 log message handler.
|
||||
///
|
||||
/// Forwards to ``log(event:)`` directly. Forwarding to the source-carrying deprecated
|
||||
/// method closed a cycle with its own default, so a handler implementing only
|
||||
/// ``log(event:)`` overflowed the stack when called through this entry point.
|
||||
@available(*, deprecated, renamed: "log(event:)")
|
||||
public func log(
|
||||
level: Logging.Logger.Level,
|
||||
@@ -290,13 +294,15 @@ extension LogHandler {
|
||||
line: UInt
|
||||
) {
|
||||
self.log(
|
||||
level: level,
|
||||
message: message,
|
||||
metadata: metadata,
|
||||
source: Logger.currentModule(filePath: file),
|
||||
file: file,
|
||||
function: function,
|
||||
line: line
|
||||
event: LogEvent(
|
||||
level: level,
|
||||
message: message,
|
||||
metadata: metadata,
|
||||
source: Logger.currentModule(filePath: file),
|
||||
file: file,
|
||||
function: function,
|
||||
line: line
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,6 +66,79 @@ struct CompatibilityTest {
|
||||
testLogging.history.assertExist(level: .error, message: "yes: error", source: "yes: error source")
|
||||
testLogging.history.assertExist(level: .critical, message: "yes: critical", source: "yes: critical source")
|
||||
}
|
||||
|
||||
/// A handler implementing only `log(event:)` must survive a caller on the SwiftLog 1.0
|
||||
/// entry point. Both deprecated defaults used to forward to each other.
|
||||
@available(*, deprecated, message: "Testing deprecated functionality")
|
||||
@Test func swiftLog1CompatibilityMethodReachesAnEventOnlyHandler() {
|
||||
let handler = EventOnlyLogHandler()
|
||||
|
||||
handler.log(
|
||||
level: .warning,
|
||||
message: "compat",
|
||||
metadata: ["k": "v"],
|
||||
file: "/Sources/CompatModule/Caller.swift",
|
||||
function: #function,
|
||||
line: 42
|
||||
)
|
||||
|
||||
let events = handler.recorded
|
||||
#expect(events.count == 1)
|
||||
#expect(events.first?.level == .warning)
|
||||
#expect(events.first?.message == "compat")
|
||||
#expect(events.first?.metadata == ["k": "v"])
|
||||
#expect(events.first?.source == "CompatModule")
|
||||
#expect(events.first?.line == 42)
|
||||
}
|
||||
|
||||
/// The same entry point must still reach a handler implementing only the
|
||||
/// source-carrying deprecated method.
|
||||
@available(*, deprecated, message: "Testing deprecated functionality")
|
||||
@Test func swiftLog1CompatibilityMethodReachesASourceOnlyHandler() {
|
||||
let testLogging = NewerButStillOldTestLogging()
|
||||
let handler = testLogging.make(label: "compat")
|
||||
|
||||
handler.log(
|
||||
level: .error,
|
||||
message: "compat",
|
||||
metadata: nil,
|
||||
file: "/Sources/CompatModule/Caller.swift",
|
||||
function: #function,
|
||||
line: 42
|
||||
)
|
||||
|
||||
testLogging.history.assertExist(level: .error, message: "compat", source: "CompatModule")
|
||||
}
|
||||
}
|
||||
|
||||
/// Implements `log(event:)` and nothing else.
|
||||
private struct EventOnlyLogHandler: LogHandler {
|
||||
private final class Store: @unchecked Sendable {
|
||||
private let lock = Lock()
|
||||
private var events: [LogEvent] = []
|
||||
func append(_ event: LogEvent) {
|
||||
self.lock.withLock { self.events.append(event) }
|
||||
}
|
||||
var all: [LogEvent] {
|
||||
self.lock.withLock { self.events }
|
||||
}
|
||||
}
|
||||
|
||||
private let store = Store()
|
||||
|
||||
var metadata: Logger.Metadata = [:]
|
||||
var logLevel: Logger.Level = .trace
|
||||
|
||||
subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
|
||||
get { self.metadata[metadataKey] }
|
||||
set { self.metadata[metadataKey] = newValue }
|
||||
}
|
||||
|
||||
func log(event: LogEvent) {
|
||||
self.store.append(event)
|
||||
}
|
||||
|
||||
var recorded: [LogEvent] { self.store.all }
|
||||
}
|
||||
|
||||
private struct OldSchoolTestLogging {
|
||||
|
||||
Reference in new issue
Block a user