mirror of
https://github.com/apple/swift-log
synced 2026-11-24 03:47:49 +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.
179 lines
6.6 KiB
Swift
179 lines
6.6 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")
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|