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.
Thanks for reading!
WRITTEN BY
Andrew Lord
A software developer and tech leader from the UK. Writing articles that focus on all aspects of Android and iOS development using Kotlin and Swift.
Want to read more?
Here are some other articles you may enjoy.