mirror of
https://github.com/apple/swift-log
synced 2026-11-23 03:44:22 +00:00
### 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>
252 lines
9.0 KiB
Swift
252 lines
9.0 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Logging API open source project
|
|
//
|
|
// Copyright (c) 2020 Apple Inc. and the Swift Logging API project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of Swift Logging API project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Not testable
|
|
import Logging
|
|
import Testing
|
|
|
|
struct CompatibilityTest {
|
|
@available(*, deprecated, message: "Testing deprecated functionality")
|
|
@Test func allLogLevelsWorkWithOldSchoolLogHandlerWorks() {
|
|
let testLogging = OldSchoolTestLogging()
|
|
|
|
var logger = Logger(label: "\(#function)", factory: { testLogging.make(label: $0) })
|
|
logger.logLevel = .trace
|
|
|
|
logger.trace("yes: trace")
|
|
logger.debug("yes: debug")
|
|
logger.info("yes: info")
|
|
logger.notice("yes: notice")
|
|
logger.warning("yes: warning")
|
|
logger.error("yes: error")
|
|
logger.critical("yes: critical", source: "any also with some new argument that isn't propagated")
|
|
|
|
// Please note that the source is _not_ propagated (because the backend doesn't support it).
|
|
testLogging.history.assertExist(level: .trace, message: "yes: trace", source: "no source")
|
|
testLogging.history.assertExist(level: .debug, message: "yes: debug", source: "no source")
|
|
testLogging.history.assertExist(level: .info, message: "yes: info", source: "no source")
|
|
testLogging.history.assertExist(level: .notice, message: "yes: notice", source: "no source")
|
|
testLogging.history.assertExist(level: .warning, message: "yes: warning", source: "no source")
|
|
testLogging.history.assertExist(level: .error, message: "yes: error", source: "no source")
|
|
testLogging.history.assertExist(level: .critical, message: "yes: critical", source: "no source")
|
|
}
|
|
|
|
@available(*, deprecated, message: "Testing deprecated functionality")
|
|
@Test func allLogLevelsWorkWithNewerButStillOldLogHandlerWorks() {
|
|
let testLogging = NewerButStillOldTestLogging()
|
|
|
|
var logger = Logger(label: "\(#function)", factory: { testLogging.make(label: $0) })
|
|
logger.logLevel = .trace
|
|
|
|
logger.trace("yes: trace", source: "yes: trace source")
|
|
logger.debug("yes: debug", source: "yes: debug source")
|
|
logger.info("yes: info", source: "yes: info source")
|
|
logger.notice("yes: notice", source: "yes: notice source")
|
|
logger.warning("yes: warning", source: "yes: warning source")
|
|
logger.error("yes: error", source: "yes: error source")
|
|
logger.critical("yes: critical", source: "yes: critical source")
|
|
|
|
// Please note that the source is _not_ propagated (because the backend doesn't support it).
|
|
testLogging.history.assertExist(level: .trace, message: "yes: trace", source: "yes: trace source")
|
|
testLogging.history.assertExist(level: .debug, message: "yes: debug", source: "yes: debug source")
|
|
testLogging.history.assertExist(level: .info, message: "yes: info", source: "yes: info source")
|
|
testLogging.history.assertExist(level: .notice, message: "yes: notice", source: "yes: notice source")
|
|
testLogging.history.assertExist(level: .warning, message: "yes: warning", source: "yes: warning source")
|
|
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 {
|
|
private let _config = Config() // shared among loggers
|
|
private let recorder = Recorder() // shared among loggers
|
|
|
|
@available(*, deprecated, message: "Testing deprecated functionality")
|
|
func make(label: String) -> any LogHandler {
|
|
OldSchoolLogHandler(
|
|
label: label,
|
|
config: self.config,
|
|
recorder: self.recorder,
|
|
metadata: [:],
|
|
logLevel: .info
|
|
)
|
|
}
|
|
|
|
var config: Config { self._config }
|
|
var history: some History { self.recorder }
|
|
}
|
|
|
|
@available(*, deprecated, message: "Testing deprecated functionality")
|
|
private struct OldSchoolLogHandler: LogHandler {
|
|
var label: String
|
|
let config: Config
|
|
let recorder: Recorder
|
|
|
|
func make(label: String) -> some LogHandler {
|
|
TestLogHandler(label: label, config: self.config, recorder: self.recorder)
|
|
}
|
|
|
|
func log(
|
|
level: Logger.Level,
|
|
message: Logger.Message,
|
|
metadata: Logger.Metadata?,
|
|
file: String,
|
|
function: String,
|
|
line: UInt
|
|
) {
|
|
self.recorder.record(level: level, metadata: metadata, message: message, source: "no source")
|
|
}
|
|
|
|
subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
|
|
get {
|
|
self.metadata[metadataKey]
|
|
}
|
|
set {
|
|
self.metadata[metadataKey] = newValue
|
|
}
|
|
}
|
|
|
|
var metadata: Logger.Metadata
|
|
|
|
var logLevel: Logger.Level
|
|
}
|
|
|
|
private struct NewerButStillOldTestLogging {
|
|
private let _config = Config() // shared among loggers
|
|
private let recorder = Recorder() // shared among loggers
|
|
|
|
@available(*, deprecated, message: "Testing deprecated functionality")
|
|
func make(label: String) -> any LogHandler {
|
|
NewerButStillOldLogHandler(
|
|
label: label,
|
|
config: self.config,
|
|
recorder: self.recorder,
|
|
metadata: [:],
|
|
logLevel: .info
|
|
)
|
|
}
|
|
|
|
var config: Config { self._config }
|
|
var history: some History { self.recorder }
|
|
}
|
|
|
|
@available(*, deprecated, message: "Testing deprecated functionality")
|
|
private struct NewerButStillOldLogHandler: LogHandler {
|
|
var label: String
|
|
let config: Config
|
|
let recorder: Recorder
|
|
|
|
func make(label: String) -> some LogHandler {
|
|
TestLogHandler(label: label, config: self.config, recorder: self.recorder)
|
|
}
|
|
|
|
func log(
|
|
level: Logger.Level,
|
|
message: Logger.Message,
|
|
metadata: Logger.Metadata?,
|
|
source: String,
|
|
file: String,
|
|
function: String,
|
|
line: UInt
|
|
) {
|
|
self.recorder.record(level: level, metadata: metadata, message: message, source: source)
|
|
}
|
|
|
|
subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
|
|
get {
|
|
self.metadata[metadataKey]
|
|
}
|
|
set {
|
|
self.metadata[metadataKey] = newValue
|
|
}
|
|
}
|
|
|
|
var metadata: Logger.Metadata
|
|
|
|
var logLevel: Logger.Level
|
|
}
|