Simpler categories when using OSLog
Organised logging in Swift that can be searched is simple thanks to the built-in os_log
. By storing our categories as an extension on OSLog
we can access them using a short syntax.
This makes categories so much easier to specify and helps with auto-completion!
extension OSLog {
static let logAuthentication = OSLog(subsystem: "com.myapp.App", category: "Auth")
static let logDatabase = OSLog(subsystem: "com.myapp.App", category: "Database")
static let logNetwork = OSLog(subsystem: "com.myapp.App", category: "Network")
}
os_log("User signed in: %@", log: .logAuthentication, type: .default, email)
os_log("Chat messages sync complete", log: .logNetwork, type: .info)
os_log("Chat messages saved successfully", log: .logDatabase, type: .debug)
I hope the article was useful. If you have any feedback or questions please feel free to reach out to me on Twitter.
Thanks for reading and happy coding!
Hi, I hope you enjoyed the article. I am Andrew - a builder of apps and developer tools. Articles on the blog focus on all aspects of Android and iOS development using Kotlin and Swift.
Subscribe via RSS
Want to read more?
Here are some other articles you may enjoy.
Clear and searchable logging in Swift with OSLog
Logging is a useful tool for diagnosing issues and working out what an app is doing. We will explore Apple's currently recommended way of logging via OSLog, covering how to use it and managing its differences when compared to other logging approaches.
Create Xcode file templates and share them with your team
When creating new files in Xcode the built-in templates often contain code we immediately delete or need to alter. We will explore the process of creating our own templates and how to go about sharing them with the rest of our team members.