Proposal discussion thread: https://forums.swift.org/t/proposal-slg-0004-metadata-value-attributes-revision-2/86488 Add an extensible per-value attribute mechanism for metadata. ### Motivation: Metadata values in `swift-log` are opaque strings by the time the `LogHandler` receives them. The call site often knows things about a value that the handler cannot infer — for example, whether the value should be redacted in different environments. ### Modifications: - SLG-0004 proposal is updated based on the feedback. - Added `AttributedStringCarrier: CustomStringConvertible`-based mechanism to `Logging`. - Added benchmarks and benchmark thresholds for the new attributed metadata. ### Result: `swift-log` provides an optional extensible mechanism for attaching attributes to metadata values. --------- Co-authored-by: Franz Busch <privat@franz-busch.de>
2.2 KiB
Logging
A unified, performant, and ergonomic logging API for Swift.
Overview
SwiftLog provides a logging API package designed to establish a common API the ecosystem can use. It allows packages to emit log messages without tying them to any specific logging implementation, while applications can choose any compatible logging backend.
SwiftLog is an API package which cuts the logging problem in half:
- A logging API (this package)
- Logging backend implementations (community-provided)
This separation allows libraries to adopt the API while applications choose any compatible logging backend implementation without requiring changes from libraries.
Getting Started
Use this package if you're writing a cross-platform application (for example, Linux and macOS) or library, and want to target this logging API.
Adding the Dependency
Add the dependency to your Package.swift:
.package(url: "https://github.com/apple/swift-log", from: "1.6.0")
And to your target:
.target(
name: "YourTarget",
dependencies: [
.product(name: "Logging", package: "swift-log")
]
)
Basic Usage
// Import the logging API
import Logging
// Create a logger with a label
let logger = Logger(label: "MyLogger")
// Use it to log messages
logger.info("Hello World!")
This outputs:
2025-10-24T17:26:47-0700 info MyLogger: [your_app] Hello World!
Default Behavior
SwiftLog provides basic console logging via StreamLogHandler. By default it
uses stdout, however, you can configure it to use stderr instead:
LoggingSystem.bootstrap(StreamLogHandler.standardError)
StreamLogHandler is primarily for convenience. For production applications,
implement the LogHandler protocol directly or use a community-maintained
backend.
Topics
Logging API
- doc:UnderstandingLoggers
LoggerLoggingSystem
Log Handlers
LogHandlerLogEventMultiplexLogHandlerStreamLogHandlerSwiftLogNoOpLogHandler