Explore Apple's new Swift logging API for iOS 14
Learn how to log events and errors in your iOS app with the new API introduced at WWDC 2020
05 Jul 2020 · 4 min read
At the WWDC 2020, Apple introduced the new Swift logging API for iOS 14. This post summarizes how to take advantage of this new API to debug behavior in your iOS apps.
When it comes to logging messages in iOS applications, the APIs that first come to your mind might be print or NSLog. Or maybe you are using a third party library like CocoaLumberjack or SwiftyBeaver.
However, already with iOS 10, Apple introduced a new iOS standard for logging with OSLog, providing an efficient way to log information.
At this year's WWDC 2020, this API became - you could say - more Swifty like. It offers different logging levels, categories, persistence, formatting, high performance and privacy features. It's worthwile checking it out.
Let's take a look at an example.
import oslet logger = Logger(subsystem: "com.tanaschita.funapp", category: "funnyanimals")let videoName = "The turtle"logger.log("Starting video with title \(videoName, privacy: .public)")
The subsystem parameter is typically the bundle identifier of your app. The category parameter can be used to structure logs from different parts of the app. You can log any type confirming to CustomStringConvertable.
The log outputs appear in Xcode's console while running the app. You can also use the Console.app from the Utilities folder on your Mac when your device is connected.
The API provides five log levels:
logger.error("Missing field \(field) in response from \(endpoint).")
Logs are archived to retrieve them later from the device. To browse through persisted messages from your device, connect the device to your Mac and open Console.app that is preinstalled on your Mac in the Utilities folder.
According to Apple, the logging system is very performant, so you can use it widely in your app without slowing it down. Unlike with print, log messages are not fully converted into a string representation. Instead, the compiler and the logging library work together to produce an optimized representation of the log message.
Nonnumeric values in your logs are configured as private by default, so they are hidden in the message. This makes sure, that after shipping your app, the logs do not show any personal information. So when using:
logger.log("Logging in user with email \(emailAddress)")
The statement above will produce the following log message:
Logging in user with email <private>
If the information you are logging should not be hidden, you have to explicitly mark it as public:
logger.log("Opening funny video \(videoId, privacy: .public)")
One thing I'm personally missing when comparing the API with the mentioned third party libraries, is the ability to retrieve all messages from the Logger instance, for example to be able to show the logs directly within the application.
Nontheless, the new API is a promising and powerful logging solution.
Like to support my work?
Articles with related topics
Quick tip on how to learn Xcode keyboard shortcuts efficiently
An easy way to memorize Xcode keyboard shortcuts
22 Sep 2019 · 2 min read
Advanced Xcode keyboard shortcuts to be more productive
Increase your productivity with these hidden Xcode shortcuts
18 Sep 2019 · 3 min read
Basic Xcode keyboard shortcuts every developer should know
Increase your productivity when working with Xcode
15 Sep 2019 · 3 min read
iOS developer guide on the main aspects of Core Data
Understand the main concepts of the Core Data framework.
19 Sep 2022 · 5 min read
Working with Xcode configuration files
Learn how to manage different iOS build environments with .xcconfig files.
21 Feb 2022 · 3 min read
Latest articles and tips
Cheat sheet on Combine operators for iOS development
Get an overview on Publisher operators in Combine.
21 Nov 2022 · 5 min read
Understanding date decoding strategies when working with JSON in Swift
Learn how to convert JSON dates to Swift's native Date type and vice versa.
14 Nov 2022 · 2 min read
How to handle non-optional Core Data properties in Swift
Explore the possibilities to use non-optional Swift values in Core Data.
07 Nov 2022 · 4 min read
Understanding different cache policies when working with URLRequest in Swift
Learn how to manage cache when sending HTTP requests in your iOS applications.
31 Oct 2022 · 4 min read
How to create NSManagedObject subsclasses for Core Data entities in Xcode
Learn about Xcode's code generation options when creating Core Data models in Swift.
24 Oct 2022 · 3 min read