In a recent series of articles, iOS independent developer Majid Jabrayilov focused on the importance of logging to enable the analysis of bugs hard to catch in the debugger as well as to better understand user behaviour through the app.
While testing an iOS app using Xcode debugger may be a reasonably good approach to ensure an app is bug-free, there are cases where this simply is not an option, says Jabrayilov.
For example, [a bug] might occur only after a few days of app usage. In this case, running an app in a simulator and testing a feature doesn’t make sense. We need to understand what the user has done in the application and how it responds to user actions in this situation.
In his articles, Jabrayilov describes a technique leveraging Apple’s Unified Logging System to make it as easy as it goes to build a proper logging system into an iOS app.
The cornerstone of this approach is the os.Logger
class, which persists all log entries to a device-based system store under a given subsystem
and category
. Logger
provides several levels of logging, including trace
, notice
, warning
, and critical
. Log entries are accessible through the Console.app on your Mac. To make all logged information easily readable in Console.app, Jabrayilov says it is convenient to use the OSLogInterpolation policies that are available beginning iOS 14. Those include options to control alignment, visibility, format, etc.
An interesting feature of Apple’s Unified Logging System is it makes it easy to export log information from a user’s device, which can become a life-saver in case a bug only shows up in the field and is not reproducible during testing.
To this aim, you define a custom OSLogStore
that is responsible for filtering all log entries matching specific criteria, including the entry category, date, and so on. As Jabrayilov shows, OSLogStore
can be easily integrated in your app to allow the user to export log entries by tapping a button and optionally share them with the developer.
Overall, Jabrayilov describes a convenient technique to add logging to any iOS app. If you are interested, do not miss the original articles, which include sample code to illustrate all these concepts in detail.