Skip to main content

Articles, snippets and thoughts about code from Andrew Lord.

Simpler categories when using OSLog

Published on · Updated on
1 minute read

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!

Like what you read? Please share the article.

Avatar of Andrew Lord

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