Extracting information from the debug console prior Xcode 15 was a bit challenging. Ever tried to find an important log entry among broken constraint logs? It was always more of a challenge rather then a seamless experience:
- No visual hints/distinction
- Metadata took a lot of space
- Searching similar to a search on a web page
Apple addressed some of those issues with the Xcode 15 release. To benefit from all new features Apple advises to use the OSLog framework for logging.
import os.log os_log("Hello, World!") os_log("The value of integer is %d", someInteger)
Color-Coded Log Levels
The first thing you’ll notice is the color-coded log levels. Each log level now appears in a distinct color, making it easier to sift through logs and identify errors or warnings at a glance. Simply launch your app, and hope there is no red you’ll see in the Debug Console :). Eg.:
Error – Typically displayed in red, indicating a serious issue that requires immediate attention:
os_log(.error, "An error occurred: %{public}@", error.localizedDescription)
Debug – Often displayed in green, giving detailed information useful for debugging purposes:
os_log(.debug, "Debug: Network request %{public}@ sent", request.description)
Default – Typically displayed in gray or white, representing general log messages:
os_log(.default, "Operation completed successfully")
Advanced Filtering
Now you can narrow down logs based on various criteria, making it easier to focus on the logs that matter. Use the filtering options to display only errors, for instance, and watch as the Debug Console filters out the rest in real-time. Beneath you’ll find some examples.
You can filter log messages based on text content, making it easy to find specific logs:
// In the Debug Console filter field, type: text:"Network Error"
If you want to see logs from a specific source, you can do so by filtering by source:
// In the Debug Console filter field, type: source:YourAppName
If there are certain logs you want to exclude, you can do so with the exclusion operator:
// In the Debug Console filter field, type: !text:"Unnecessary Log"
Refined Message Display and Metadata Inspection
The message display is more organized, and metadata inspection is easy. Hover over a log message, and a tooltip will display the associated metadata. You can also enable selected metadata to be shown “inline” above the log message. Some additional capabilities are:
Custom Metadata – You can also log custom metadata along with your log messages:
var log = OSLog(subsystem: "com.example.MyApp", category: "Networking") os_log("Network request failed", log: log, type: .error) // In Debug Console: [2023-10-15 10:00:00] [Networking] Network request failed
Formatted Messages – Formatted messages allow for a more structured and readable log output:
os_log("User %{public}@ logged in with role %{public}@", user.name, user.role) // In Debug Console: User John Doe logged in with role Admin
Category Filtering – Categorize log messages and filter them in the Debug Console based on categories:
var log = OSLog(subsystem: "com.example.MyApp", category: "Authentication") os_log("Authentication successful", log: log) // In Debug Console, filter by category: Authentication
Also there is finally On-Device-Debugging. Just connect your device, hit the debug button and your logs will appear in the console.
The new Debug Console in Xcode 15 is a substantial upgrade, providing developers with robust and intuitive tools for effective debugging, solving common developer pain points with:
- Color-coded logs for instant issue recognition.
- Advanced filters to narrow down critical logs.
- On-device debugging for seamless insights.
Start implementing these powerful logging techniques in your next project, and witness firsthand how they transform your debugging workflow.
Remember, the examples provided are just the beginning. As you explore further, you’ll uncover even more functionalities that will elevate your debugging game in Xcode 15. Happy coding and debugging!
We appreciate the opportunity to hear about your experience. Contact us here.