Debugging applications can lead to crammed implementation of print and debug statements within controllers and functions, making it difficult to realize the crucial bits. This is a super simple logging framework that can be added and applied throughout an iOS application to get key information. The framework uses a simple function that provides:
- message
- event
- file name
- line
- column
- function name
This is a simple Cocoa framework, and can be added to iOS application like others - Apple docs do a good job at explaining how to add frameworks. However in a few lines:
- Within your application, navigate to Build Phases -> Link Binaries with Libraries
- Drag the Logger.framework file into the area
- Also add in the embedded binaries section
After these steps, you can simply import Logger in your files and call the Logger.log function:
-
message: whatever you would like to be printed when this logger is encountered.
-
LogEvent: this is an enumeration with several different cases as described below (meant to represent common categories):
- case e - Error
- case i - Information
- case d - Debug
- case v - Verbose
- case w - Warning
- case s - Severe
The output is easy to read with all the necessary information:
The logger works particularly in situations where we might put print statements to test if things are going well, or in case they don't. For example, the do-catch block in URLSession based networking code, or if-let and guard wrappers for optionals.