mirror of
https://github.com/apple/swift-log
synced 2026-11-24 03:47:49 +00:00
This proposal adds task-local logger storage so that metadata can accumulate across async call stacks without threading a `Logger` through every function signature. ### Motivation: Metadata propagation requires threading a logger through every layer. Every function on the path from request ingress to the bottom of the call stack has to accept, mutate, and forward a `Logger`. Libraries must choose between polluting their public API with a `logger:` parameter and losing all the caller's context. There is no third option today. ### Modifications: - New API surface for reading and modifying the `@TaskLocal` `Logger` instance added. - Best practices on how to correctly propagate logger into libraries updated with `Logger.current` API. ### Result: Users can now automatically propagate accumulated metadata into libraries without explicitly passing loggers through the API boundaries. --------- Co-authored-by: Franz Busch <privat@franz-busch.de>
67 lines
2.0 KiB
Swift
67 lines
2.0 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Logging API open source project
|
|
//
|
|
// Copyright (c) 2025 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 Benchmark
|
|
import Foundation
|
|
import Logging
|
|
|
|
public func makeBenchmark(
|
|
loggerLevel: Logger.Level,
|
|
logLevel: Logger.Level,
|
|
setScopedLogger: Bool = false,
|
|
_ suffix: String = "",
|
|
_ body: @escaping (Logger) -> Void
|
|
) {
|
|
let iterations = 1_000_000
|
|
let metrics: [BenchmarkMetric] = [.instructions, .objectAllocCount]
|
|
|
|
var logger = Logger(label: "BenchmarkRunner_\(loggerLevel)_\(logLevel)")
|
|
|
|
// Use a NoOpLogHandler to avoid polluting the logs
|
|
logger.handler = NoOpLogHandler(label: "NoOpLogHandler")
|
|
logger.logLevel = loggerLevel
|
|
|
|
Benchmark(
|
|
"\(logLevel)_log_with_\(loggerLevel)_log_level\(suffix)",
|
|
configuration: .init(
|
|
metrics: metrics,
|
|
maxIterations: iterations,
|
|
thresholds: [
|
|
.instructions: BenchmarkThresholds(
|
|
relative: [
|
|
.p90: 10.0 // we only record p90
|
|
]
|
|
),
|
|
.objectAllocCount: BenchmarkThresholds(
|
|
absolute: [
|
|
.p90: 0 // we only record p90
|
|
]
|
|
),
|
|
]
|
|
)
|
|
) { benchmark in
|
|
if setScopedLogger {
|
|
withLogger(logger) { logger in
|
|
benchmark.startMeasurement()
|
|
body(logger)
|
|
benchmark.stopMeasurement()
|
|
}
|
|
} else {
|
|
benchmark.startMeasurement()
|
|
body(logger)
|
|
benchmark.stopMeasurement()
|
|
}
|
|
}
|
|
}
|