Viewing debug level logs from iOS simulator

Unified logging system (aka os_log)

The unified logging system (aka os_log) is the recommended way of logging by Apple. It’s pretty easy to create a logger and log messages at different levels:

import SwiftUI
import os.log

extension Logger {
static let button = Logger(subsystem: "name.haosong.OSLogDemo", category: "Button")
}

struct ContentView: View {
var body: some View {
VStack {
Button("Debug", action: {
Logger.button.log(level: .debug, "debug log")
}).padding()

Button("Info", action: {
Logger.button.log(level: .info, "info log")
}).padding()

// ...
}
}
}

On macOS, we can view the log messages using the Console app. But the debug level messages from iOS simulators are not visible even after turning on the Include Info Messages and Include Debug Messages in Action menu of the Console app.

This could be a bug of the simulator, because debug level messages from iOS devices are not affected in the Console app.

Streaming debug logs from the simulator to Terminal

The workaround is to stream the log from the simulator to Terminal:

# if the simulator is already booted
$ xcrun simctl spawn booted log stream --level debug --predicate 'subsystem == "name.haosong.OSLogDemo"'

# or you can use the device UUID
$ xcrun simctl spawn <DEVICE-UUID> log stream --level debug --predicate 'subsystem == "name.haosong.OSLogDemo"'

References