Swift:
Logging

How to:

In Swift, you can write logs to the console with print statements or the more flexible os.log API, which hooks into the Unified Logging System on Apple platforms.

import os.log

let logger = OSLog(subsystem: "com.yourapp.domain", category: "network")

func fetchData() {
    // Simple print statement
    print("Fetch started")
    
    // Logging info-level event using os.log
    os_log(.info, log: logger, "Fetching data from API.")
    
    do {
        let data = try performNetworkRequest()
        // Logging debug-level event
        os_log(.debug, log: logger, "Data received: %@", data.description)
    } catch {
        // Logging error-level event
        os_log(.error, log: logger, "Failed to fetch data: %@", error.localizedDescription)
    }
}

func performNetworkRequest() throws -> Data {
    // Simulate a network request
    return Data()
}

Sample output on the console might look like this:

Fetch started
Fetching data from API.
Data received: Some data bytes...

For errors, it might be:

Failed to fetch data: The Internet connection appears to be offline.

Deep Dive

Logging in Swift takes on new power and efficiency with the Unified Logging System introduced in iOS 10 and macOS Sierra. Unlike the print statement that goes straight to the console, this system is activity-based, and allows you to filter log messages based on their importance and whether they’re debug or release builds.

The historical context frames the evolution of logging in iOS and macOS from rudimentary print statements towards comprehensive tools that integrate with the Instruments app and Console, providing sophisticated ways to analyze logs.

There are a range of alternatives to logging within Swift, such as third-party libraries like CocoaLumberjack, which offers a macro layer over the Unified Logging System. It provides enhanced control over log formatting, file management, and performance options.

Lastly, implementation details; OSLog is designed not only to be efficient but also privacy-conscious, with the ability to obfuscate private data when logging. It categorizes logs into fault, error, info, and debug levels, each offering a different granularity for troubleshooting.

See Also