`StreamLogHandler` timestamps only have second precision, so when several log lines land in the same second you can't tell what order they happened in. This adds an **opt-in** setting for millisecond precision. The default output doesn't change, since some users parse it. **Changes** - Adds `public var timestampPrecision: TimestampPrecision = .seconds` to `StreamLogHandler`. - Adds a nested `TimestampPrecision` enum (`@nonexhaustive`, `Sendable`) with `.seconds` and `.milliseconds`. - With `.milliseconds`, the timestamp includes a 3-digit millisecond field: - default (`.seconds`): `2021-11-06T18:47:14+0300 info myapp: message` - `.milliseconds`: `2021-11-06T18:47:14.123+0300 info myapp: message` - How each platform reads the time: - Darwin, Linux and Android use `clock_gettime(CLOCK_REALTIME)`. - Windows uses `_ftime64_s`. - WASI only has second resolution (`CLOCK_REALTIME` isn't importable there), so `.milliseconds` always prints `.000`. The doc comments say so. - Both new symbols are added to the DocC reference page. **Tests** - The default precision is `.seconds`. - `.milliseconds` produces the expected format (checked with a regex). - Milliseconds are always zero-padded to 3 digits. - The existing output-format tests are unchanged and still pass, which confirms the default format. Fixes #212
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.2
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.