SwiftyBeaver Tutorial for iOS: A Logging Platform for Swift
In this Swift tutorial, you’ll learn how to integrate SwiftyBeaver logging into your iOS application. By Ted Bendixson.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
SwiftyBeaver Tutorial for iOS: A Logging Platform for Swift
25 mins
- Getting Started
- Using The Starter App
- Installing SwiftyBeaver
- Writing Your First Logs With SwiftyBeaver
- A Brief Explanation of Log Levels
- How to Write to Different Log Levels With SwiftyBeaver
- Setting Up The SwiftyBeaver Crypto Cloud
- Filtering Logs By Log Level, Favorites, and Minimum Log Levels
- Filtering Logs With Minimum Log Levels
- Fixing Hard-to-Reach Bugs
- Simulating a Bug
- Bug Sleuthing With SwiftyBeaver
- Where to Go From Here?
A Brief Explanation of Log Levels
At this point, I am sure you’re wondering why SwiftyBeaver makes you use a method named info()
instead of the far more intuitive log()
or print()
.
It has to do with something called Log Levels.
Not everything you log is equally important. Some logs are useful for giving the programmer a bit of extra contextual information. Other logs communicate more serious issues like errors and warnings.
When reading through a log file, it is especially helpful to have log messages that are categorized by the threat they pose to your application. It helps you filter through the less important messages quickly so you can fix bugs faster.
SwiftyBeaver sticks to log level conventions and adopts these five log levels:
- Verbose: The lowest priority level. Use this one for contextual information.
- Debug: Use this level for printing variables and results that will help you fix a bug or solve a problem.
- Info: This is typically used for information useful in a more general support context. In other words, info that is useful for non developers looking into issues.
- Warning: Use this log level when reaching a condition that won’t necessarily cause a problem but strongly leads the app in that direction.
- Error: The most serious and highest priority log level. Use this only when your app has triggered a serious error.
How to Write to Different Log Levels With SwiftyBeaver
Just as you used SwiftyBeaver’s info()
method to log to the info log level, you can use the other four methods — verbose()
, debug()
, warning()
, and error()
— to log the other four levels.
Give it a try. Back in application(_:didFinishLaunchingWithOptions:)
, replace the call to info()
with this:
SwiftyBeaver.debug("Look ma! I am logging to the DEBUG level.")
Now build and run your app. You should see a different colored heart icon and a log message at the debug level.
14:48:14.155 💚 DEBUG AppDelegate.application():36 - Look ma! I am logging to the DEBUG level.
Notice that the color of the icon changed to green to indicate the Debug log level. It’s one of the reasons why SwiftyBeaver is better than print()
and NSLog()
. You can quickly scan your logs to find messages at the log level you are interested in.
Note: Try not to abuse the higher priority log levels. Warnings and errors should be reserved for situations that require attention. It’s just like that old saying, “When everything is urgent, nothing is.”
Note: Try not to abuse the higher priority log levels. Warnings and errors should be reserved for situations that require attention. It’s just like that old saying, “When everything is urgent, nothing is.”
Setting Up The SwiftyBeaver Crypto Cloud
One of the coolest features in SwiftyBeaver is the ability to log directly to the cloud. SwiftyBeaver comes with a macOS App that lets you view your logs in real time. If you have ever wondered what’s happening in your app that is installed on thousands of devices, now you can know.
Download The SwiftyBeaver Mac App. Open the app, and fill out the form to create an account.
Next, you will be taken directly to a dialog box that asks you to save a file. It’s a little strange because they don’t tell you what the file is for.
Name it TenPMLogs, chose any location you prefer and hit Save.
The file you created stores the logs for a single app, which is why you named it TenPMLogs. When you open this file using the SwiftyBeaver Mac App, you can view the logs associated with the Ten PM app.
Once you have saved your log file, you will be presented with a choice. You can either register a new app, or you can view logs from a previously registered app. You’ll continue with a New App.
Click on the Generate New App Credentials button. You should see the following screen, showing the generated ID and keys for your app:
You are going to add another log destination using the Crypto Cloud security credentials just generated. Keep this window open, and go back to setupSwiftyBeaverLogging()
in AppDelegate.swift.
Add these lines to the bottom of the method, substituting the string values shown in the macOS app for the stand-in strings below:
let platform = SBPlatformDestination(appID: "Enter App ID Here",
appSecret: "Enter App Secret Here",
encryptionKey: "Enter Encryption Key Here")
SwiftyBeaver.addDestination(platform)
Go back to the SwiftyBeaver Mac App and click on the Connect button. Then build and run Ten PM.
Note: If you are a silly fool like me, and you clicked the Connect button before copying your credentials into your app, you can click on the settings gear in the SwiftyBeaver Mac App to view them after connecting.
Note: If you are a silly fool like me, and you clicked the Connect button before copying your credentials into your app, you can click on the settings gear in the SwiftyBeaver Mac App to view them after connecting.
Check it out! Your log message now appears in the SwiftyBeaver Mac App. If you don’t see the logs right away, don’t despair. Sometimes, it can take a few minutes for the log messages to get to the cloud. They will show up eventually. You can see that I anxiously relaunched the app to see if it might make my log messages appear faster.
SwiftyBeaver also puts an automatic one hour expiration on all logs you generate — that is, unless you upgrade to a paid account. For most debugging tasks, this won’t be an issue. But it is worth mentioning in case you ever wonder why your older logs aren’t visible any more.
Filtering Logs By Log Level, Favorites, and Minimum Log Levels
The truly great thing about the SwiftyBeaver Mac App is the ability to filter your logs by log level. It dramatically simplifies the process of digging through log files to find the cause of a critical bug.
You may have noticed the different tabs up at the top. Each of those tabs represents a different log level. You can view multiple log levels at a time, or you can drill down and look at just the warnings and errors.
You can also star a log to mark it as a Favorite. You can view all favorites by clicking on the star in the left hand menu.