### Motivation: The two deprecated `LogHandler` defaults forward to each other: the source-carrying `log(level:message:metadata:source:file:function:line:)` default calls `log(event:)`, whose default calls the source-carrying method, and the SwiftLog 1.0 `log(level:message:metadata:file:function:line:)` default calls the source-carrying method. A handler that implements only `log(event:)`, as the protocol documentation asks, therefore recurses until the stack overflows whenever anything calls the 1.0 method on it. Handlers written against SwiftLog 1.0 that wrap another handler and forward through the 1.0 method reach this path. Differs from #248, where the handler implemented no log method at all. ### Modifications: The 1.0 default builds a `LogEvent` and calls `log(event:)` directly. The source is still the module derived from `file`, as before. Two tests in `CompatibilityTest`: the 1.0 method reaching an event-only handler (SIGSEGV at `main` before the change) and reaching a source-only handler. ### Result: A `LogHandler` implementing only `log(event:)` can be called through every deprecated entry point. No source-breaking change; both deprecated defaults remain. Verified on Linux with Swift 6.1.2: `swift test -Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable` (166 tests) and `swift-format lint --strict` on the changed files. Found by an automated audit loop (Claude); patch and description reviewed by me. --------- Co-authored-by: Vladimir Kukushkin <kukushechkin@apple.com>
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.