mirror of
https://github.com/apple/swift-log
synced 2026-11-29 04:14:22 +00:00
Avoid logging warnings when handler does not support metadataproviders (#252)
This commit is contained in:
1 parent
3e3ef75109
commit
ba2a9f5b93
3 files changed
+54
-5
No files matched your search
@@ -177,7 +177,11 @@ extension LogHandler {
|
||||
nil
|
||||
}
|
||||
set {
|
||||
self.log(level: .warning, message: "Attempted to set metadataProvider on \(Self.self) that did not implement support for them. Please contact the log handler maintainer to implement metadata provider support.", metadata: nil, source: "Logging", file: #file, function: #function, line: #line)
|
||||
#if DEBUG
|
||||
if LoggingSystem.warnOnceLogHandlerNotSupportedMetadataProvider(Self.self) {
|
||||
self.log(level: .warning, message: "Attempted to set metadataProvider on \(Self.self) that did not implement support for them. Please contact the log handler maintainer to implement metadata provider support.", metadata: nil, source: "Logging", file: #file, function: #function, line: #line)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -663,6 +663,10 @@ public enum LoggingSystem {
|
||||
private static let _factory = FactoryBox { label, _ in StreamLogHandler.standardOutput(label: label) }
|
||||
private static let _metadataProviderFactory = MetadataProviderBox(nil)
|
||||
|
||||
#if DEBUG
|
||||
private static var _warnOnceBox: WarnOnceBox = WarnOnceBox()
|
||||
#endif
|
||||
|
||||
/// `bootstrap` is a one-time configuration function which globally selects the desired logging backend
|
||||
/// implementation. `bootstrap` can be called at maximum once in any given program, calling it more than once will
|
||||
/// lead to undefined behavior, most likely a crash.
|
||||
@@ -708,9 +712,7 @@ public enum LoggingSystem {
|
||||
|
||||
fileprivate static var factory: (String, Logger.MetadataProvider?) -> LogHandler {
|
||||
return { label, metadataProvider in
|
||||
var handler = self._factory.underlying(label, metadataProvider)
|
||||
handler.metadataProvider = metadataProvider
|
||||
return handler
|
||||
self._factory.underlying(label, metadataProvider)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -727,6 +729,15 @@ public enum LoggingSystem {
|
||||
return self._metadataProviderFactory.metadataProvider
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
/// Used to warn only once about a specific ``LogHandler`` type when it does not support ``Logger/MetadataProvider``,
|
||||
/// but an attempt was made to set a metadata provider on such handler. In order to avoid flooding the system with
|
||||
/// warnings such warning is only emitted in debug mode, and even then at-most once for a handler type.
|
||||
internal static func warnOnceLogHandlerNotSupportedMetadataProvider<Handler: LogHandler>(_ type: Handler.Type) -> Bool {
|
||||
self._warnOnceBox.warnOnceLogHandlerNotSupportedMetadataProvider(type: type)
|
||||
}
|
||||
#endif
|
||||
|
||||
private final class FactoryBox {
|
||||
private let lock = ReadWriteLock()
|
||||
fileprivate var _underlying: (_ label: String, _ provider: Logger.MetadataProvider?) -> LogHandler
|
||||
@@ -1439,6 +1450,28 @@ extension Logger.MetadataValue: ExpressibleByArrayLiteral {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Debug only warnings
|
||||
|
||||
#if DEBUG
|
||||
/// Contains state to manage all kinds of "warn only once" warnings which the logging system may want to issue.
|
||||
private final class WarnOnceBox {
|
||||
private let lock: Lock = Lock()
|
||||
private var warnOnceLogHandlerNotSupportedMetadataProviderPerType: [ObjectIdentifier: Bool] = [:]
|
||||
|
||||
func warnOnceLogHandlerNotSupportedMetadataProvider<Handler: LogHandler>(type: Handler.Type) -> Bool {
|
||||
self.lock.withLock {
|
||||
let id = ObjectIdentifier(type)
|
||||
if warnOnceLogHandlerNotSupportedMetadataProviderPerType[id] ?? false {
|
||||
return false // don't warn, it was already warned about
|
||||
} else {
|
||||
warnOnceLogHandlerNotSupportedMetadataProviderPerType[id] = true
|
||||
return true // warn about this handler type, it is the first time we encountered it
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// MARK: - Sendable support helpers
|
||||
|
||||
#if compiler(>=5.7.0)
|
||||
|
||||
@@ -25,7 +25,9 @@ import Glibc
|
||||
final class MetadataProviderTest: XCTestCase {
|
||||
func testLoggingMergesOneOffMetadataWithProvidedMetadataFromExplicitlyPassed() throws {
|
||||
let logging = TestLogging()
|
||||
LoggingSystem.bootstrapInternal(logging.make)
|
||||
LoggingSystem.bootstrapInternal(logging.makeWithMetadataProvider,
|
||||
metadataProvider: .init { ["common": "initial"]
|
||||
})
|
||||
|
||||
let logger = Logger(label: #function, metadataProvider: .init {
|
||||
[
|
||||
@@ -42,21 +44,31 @@ final class MetadataProviderTest: XCTestCase {
|
||||
}
|
||||
|
||||
func testLogHandlerThatDidNotImplementProvidersButSomeoneAttemptsToSetOneOnIt() {
|
||||
#if DEBUG
|
||||
// we only emit these warnings in debug mode
|
||||
let logging = TestLogging()
|
||||
var handler = LogHandlerThatDidNotImplementMetadataProviders(testLogging: logging)
|
||||
|
||||
handler.metadataProvider = .simpleTestProvider
|
||||
|
||||
logging.history.assertExist(level: .warning, message: "Attempted to set metadataProvider on LogHandlerThatDidNotImplementMetadataProviders that did not implement support for them. Please contact the log handler maintainer to implement metadata provider support.", source: "Logging")
|
||||
|
||||
let countBefore = logging.history.entries.count
|
||||
handler.metadataProvider = .simpleTestProvider
|
||||
XCTAssertEqual(countBefore, logging.history.entries.count, "Should only log the warning once")
|
||||
#endif
|
||||
}
|
||||
|
||||
func testLogHandlerThatDidImplementProvidersButSomeoneAttemptsToSetOneOnIt() {
|
||||
#if DEBUG
|
||||
// we only emit these warnings in debug mode
|
||||
let logging = TestLogging()
|
||||
var handler = LogHandlerThatDidImplementMetadataProviders(testLogging: logging)
|
||||
|
||||
handler.metadataProvider = .simpleTestProvider
|
||||
|
||||
logging.history.assertNotExist(level: .warning, message: "Attempted to set metadataProvider on LogHandlerThatDidImplementMetadataProviders that did not implement support for them. Please contact the log handler maintainer to implement metadata provider support.", source: "Logging")
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user