mirror of
https://github.com/apple/swift-log
synced 2026-11-23 03:44:22 +00:00
Replace the flat-parameter `log(level:message:metadata:source:file:function:line:)` method on `LogHandler` with `log(event: LogEvent)`, enabling forward-compatible evolution of the `LogHandler` interface without breaking existing handler implementations. ### Motivation: Changes to the `LogHandler` API require dance around creating new overloads, deprecating old overloads, which unnecessary bloats `LogHandler` protocol surface (like happened with the source: parameter). We have upcoming changes that would introduce new capabilities to the `LogHandler` protocol, for example https://forums.swift.org/t/proposal-slg-0003-standardized-error-metadata-via-logger-convenience/84518/35. It makes sense to introduce a single container structure for all the data associated with a log event before introducing any new overloads in `LogHandler` protocol. ### Modifications: - New struct `LogEvent` is added. - `LogHandler` protocol now has an additional `log(event: LogEvent)` method. - `Logger` is calling handler's `log(event: LogEvent)` method. ### Result: Further changes to the `LogEvent` with new information passed from the `Logger` to a `LogHandler` does not require changes in the `LogHandler` API. As a bonus, `LogEvent` makes `source` a computable variable, improving performance for log handlers who do not need the default source created based on the log file location.
126 lines
3.6 KiB
Swift
126 lines
3.6 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Logging API open source project
|
|
//
|
|
// Copyright (c) 2018-2019 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Testing
|
|
|
|
@testable import Logging
|
|
|
|
#if canImport(Darwin)
|
|
import Darwin
|
|
#elseif os(Windows)
|
|
import WinSDK
|
|
#elseif canImport(Android)
|
|
import Android
|
|
#else
|
|
import Glibc
|
|
#endif
|
|
|
|
struct MetadataProviderTest {
|
|
@Test func logHandlerThatDidNotImplementProvidersButSomeoneAttemptsToSetOneOnIt() {
|
|
#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
|
|
#expect(countBefore == logging.history.entries.count, "Should only log the warning once")
|
|
#endif
|
|
}
|
|
|
|
@Test func logHandlerThatDidImplementProvidersButSomeoneAttemptsToSetOneOnIt() {
|
|
#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
|
|
}
|
|
}
|
|
|
|
extension Logger.MetadataProvider {
|
|
static var simpleTestProvider: Logger.MetadataProvider {
|
|
Logger.MetadataProvider {
|
|
["test": "provided"]
|
|
}
|
|
}
|
|
}
|
|
|
|
struct LogHandlerThatDidNotImplementMetadataProviders: LogHandler {
|
|
let testLogging: TestLogging
|
|
init(testLogging: TestLogging) {
|
|
self.testLogging = testLogging
|
|
}
|
|
|
|
subscript(metadataKey _: String) -> Logging.Logger.Metadata.Value? {
|
|
get {
|
|
nil
|
|
}
|
|
set(newValue) {
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
var metadata: Logging.Logger.Metadata = [:]
|
|
|
|
var logLevel: Logging.Logger.Level = .trace
|
|
|
|
func log(event: LogEvent) {
|
|
self.testLogging.make(label: "fake").log(event: event)
|
|
}
|
|
}
|
|
|
|
struct LogHandlerThatDidImplementMetadataProviders: LogHandler {
|
|
let testLogging: TestLogging
|
|
init(testLogging: TestLogging) {
|
|
self.testLogging = testLogging
|
|
}
|
|
|
|
subscript(metadataKey _: String) -> Logging.Logger.Metadata.Value? {
|
|
get {
|
|
nil
|
|
}
|
|
set(newValue) {
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
var metadata: Logging.Logger.Metadata = [:]
|
|
|
|
var logLevel: Logging.Logger.Level = .trace
|
|
|
|
var metadataProvider: Logger.MetadataProvider?
|
|
|
|
func log(event: LogEvent) {
|
|
self.testLogging.make(label: "fake").log(event: event)
|
|
}
|
|
}
|