Files
swift-log-oslog/Sources/Logging OSLog/Docs.docc/index.md
2026-03-02 19:37:55 +01:00

3.2 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"),
	]

	// And using loggers
	logger["main"]!.debug("Starting build",
		metadata: [
			"input.output_directory": "\(output)",
			"input.data": "\(data ?? "-")"
	])
}

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.

Metadata is appended as JSON to the logged message after an ASCII right arrow ("->").

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