Only quote leaf metadata values. ### Motivation: There are several issues with how nested `MetadataValue`s are handled: 1. For highly nested (10k+) metadata, `.description` and `==` operator hit stack overflow. This is probably not very realistic depth, but still. 2. For not so highly nested metadata (starting at about 20) `.description` and `==` operator get very computationally intensive because of re-escaping of the already escaped children. This makes the above stack overflow crash not reachable in practice. 3. This also results in simple structured metadata to be rendered something like `["[\"[\\\"[\\\\\\\"[\\\\\\\\\\\\\\\"leaf\\\\\\\\\\\\\\\"]\\\\\\\"]\\\"]\"]"]`. 4. Finally, even after solving the re-quoting and stack over flow on rendering, there is still an issue of [recursion in `.deinit`](https://github.com/swiftlang/swift/issues/58080), resulting in a _different_ stack overflow crash. This PR provides a fix for 2 and 3, leaving 1 and 4 out because there is no practical way of fixing it in this package, while the case is borderline unrealistic. ### Modifications: - Only quote left `String` and `StringRepresentable` values in `Logger.MetadataValue.description`. - Add benchmarks for metadata values description rendering. ### Result: 100 nested metadata values render cleanly. Resolves #515 ### Alternatives This is discussable if this fix is really necessary. Iterative approach, because of allocations, is slower than recursion. - Is it worth fixing the recursion, or a metadata depth check is enough? - Is it worth fixing the nested metadata formatting?
SwiftLog
This repository contains a logging API implementation for Swift. SwiftLog provides a unified, performant, and ergonomic logging API that can be adopted by libraries and applications across the Swift ecosystem.
- 📚 Documentation and tutorials are available on the Swift Package Index
- 🚀 Contributions are welcome, please see CONTRIBUTING.md
- 🪪 License is Apache 2.0, repeated in LICENSE.txt
- 🔒 Security issues should be reported via the process in Security
- 🔀 Available Logging Backends: SwiftLog is an API package - you'll want to choose from the many community-maintained logging backends for production use
Quick Start
The following snippet shows how to add SwiftLog to your Swift Package:
// swift-tools-version: 6.1
import PackageDescription
let package = Package(
name: "YourApp",
dependencies: [
.package(url: "https://github.com/apple/swift-log", from: "1.6.0")
],
targets: [
.target(
name: "YourApp",
dependencies: [
.product(name: "Logging", package: "swift-log")
]
)
]
)
Then start logging:
import Logging
// Create a logger
let logger = Logger(label: "com.example.YourApp")
// Log at different levels
logger.info("Application started")
logger.warning("This is a warning")
logger.error("Something went wrong", metadata: ["error": "\(error)"])
// Add metadata for context
var requestLogger = logger
requestLogger[metadataKey: "request-id"] = "\(UUID())"
requestLogger.info("Processing request")
Available log handler backends
The community has built numerous specialized logging backends.
A great way to discover available log backend implementations is searching the
Swift Package Index
for the swift-log keyword.
Building with CMake
While Swift Package Manager (SPM) is the primary build system for development and testing, CMake build support is also available for integrated or mixed build environments if needed. Note that tests are not included in the CMake build, they are managed through Swift Package Manager with swift test.