Files
swift-log-oslog/Sources/Logging OSLog/Docs.docc/index.md
T. R. Bernstein 48fdc144c1 Fix message reduction error
The unified logging system (aka OSLog) expects a OSLogMessage and does
not allow just passing in a string variable. Instead the variable has to
be interpolated into a string literal, which sets the default privacy
setting to 'private' and hence reducts the whole message.
This commit sets the privacy setting to 'public'.
2026-03-02 16:06:13 +01:00

2.9 KiB

LoggingOSLog

An unified logging backend implementation for SwiftLog.

Overview

LoggingOSLog is a logging backend implementation for SwiftLog, that routes log messages to OSLog, the unified logging system on Apple platforms. On macOS you can view the log messages using the Console app or the log cli tool.

While SwiftLog is available on all Swift platforms though, the unified logging system of OSLog is only available on Apple platforms. Use this library to combine both worlds. Use SwiftLog in your codebase and use this library as the backend implementation when compiling for an Apple platform, i.e. using #if canImport(Darwin).

Getting started

Use this package if you're writing a cross-platform (for example, Linux and macOS) application or library and still want to take full advantage of the unified logging system on Apple platforms.

Adding the Dependency

Add the dependency to your Package.swift:

.package(url: "https://github.com/astzweig/swift-log-oslog", from: "1.0.0")

And to your target:

.target(
	name: "YourTarget",
	dependencies: [
		.product(name: "LoggingOSLog", package: "swift-log-oslog")
	]
)

Basic Usage

// Import the logging API and this backend implementation
import Logging
#if canImport(Darwin)
import LoggingOSLog
#endif

// Later, in your application initialization code
func init() {
	// ...
	#if canImport(Darwin)
	LoggingSystem.bootstrap(LoggingOSLog.init)
	#endif

	// Start creating loggers
	let loggers: [String: Logger] = [
		"main": Logger(label: "com.example.yourapp.Main"),
		"mail": Logger(label: "com.example.yourapp.Mail System"),
	]
}

Relation to the unified logging system

The unified logging system uses two parameters to allow for better filtering of log messages: subsystem and category. SwiftLog has only a label parameter, so this library maps a reverse domain style label to the subsystem and category parameters of the unified logging system. See /LoggingOSLog/LoggingOSLog/init(label:) for more information.

What this library does not supply

This library does not implement any of the interpolation privacy features of the unified logging system, i.e. there is no way to redact or align a meta information, like the Logging API would allow.

Topics

API

  • LoggingOSLog