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?
Filtering Logs With Minimum Log Levels
There’s one more feature you’re really going to love. SwiftyBeaver lets you set a minimum log level for a given log destination. If you want to reserve your Crypto Cloud account for serious warnings and errors, you can do that.
First, replace the debug log statement in applicationDidFinishLaunching()
with the following:
SwiftyBeaver.verbose("Watch me bloviate. I'm definitely not important enough for the cloud.")
SwiftyBeaver.debug("I am more important, but the cloud still doesn't care.")
SwiftyBeaver.info("Why doesn't the crypto cloud love me?")
SwiftyBeaver.warning("I am serious enough for you to see!")
SwiftyBeaver.error("Of course I'm going to show up in your console. You need to handle this.")
Now you’re logging a message at every level. Build and run the app, and you should see all of the logs make it up to Crypto Cloud.
In setupSwiftyBeaverLogging()
, add the following right before you add the platform logging destination:
platform.minLevel = .warning
Build and run the app again. Take another look at your Crypto Cloud console.
You should only see the warnings and errors for the latest timestamp. None of the other log statements will have made it to the Crypto Cloud console. You will still see everything in the Xcode console!
Note: You can specify a minimum log level for any type of SwiftyBeaver logging destination. You can even create multiple Crypto Cloud destinations for different log levels just to keep things nice and separate. SwiftyBeaver has a lot of wiggle room for customization.
Note: You can specify a minimum log level for any type of SwiftyBeaver logging destination. You can even create multiple Crypto Cloud destinations for different log levels just to keep things nice and separate. SwiftyBeaver has a lot of wiggle room for customization.
Fixing Hard-to-Reach Bugs
It’s been fun to talk about logging to Crypto Cloud, but you have some sinister bugs you need to fix — or at least, some sinister bugs that you are going to intentionally simulate and then fix.
Start by cleaning up some of your earlier logs. Remove all of the logs from application(_:didFinishLaunchingWithOptions:)
. Also delete the platform.minLevel
setting so that the default of printing all logs occurs.
For this test, you’ll want to see all logs.
Simulating a Bug
Now that you are ready to go with cloud logging, it’s time to simulate a nasty background bug.
Open LocationTracker.swift and find locationManager(_:didUpdateLocations:)
. Insert the following code right after the two guard
statements at the top:
let bug = true
if bug == true {
return
}
This is rather silly, but you are pretending there’s a bug somewhere in the LocationTracker
preventing it from tracking the user’s location. The placement here prevents the notifications that indicate the user has entered or left the safe zone from posting. When the bug is “turned off”, the behavior will function normally.
Build and run to confirm the issue.
As long as you left the location in the custom area used previously, you’ll see the bug. Although you are clearly outside of the safe zone, the text is indicating you are a safe distance from home.
Bug Sleuthing With SwiftyBeaver
Now how might you do some sleuthing with SwiftyBeaver?
If you received error reports about tracking failures, and had no good guesses on the bug, put log statements everywhere to gather as much information as possible from impacted users.
First, go up to the top of the LocationTracker
class and import SwiftyBeaver:
import SwiftyBeaver
Next, put a log statement at the top of the locationManager(_:didUpdateLocations:)
:
SwiftyBeaver.info("Got to the didUpdateLocations() method")
Now go a few lines down and paste this line of code right after the line where you declare bug
:
SwiftyBeaver.debug("The value of bug is: \(bug)")
Next add another inside of the conditional where you check for the presence of the bug, just before the return
:
SwiftyBeaver.error("There's definitely a bug... Aborting.")
And finally, put one more log at the end of locationManager(_:didUpdateLocations:)
:
SwiftyBeaver.info("Got to the end the didUpdateLocations method")
That ought to be enough information to start figuring out what’s going on in your code. For reference, here is what the entire contents of locationManager(_:didUpdateLocations:)
should look like:
SwiftyBeaver.info("Got to the didUpdateLocations() method")
guard let homeLocation = ParentProfile.homeLocation else {
ParentProfile.homeLocation = locations.first
return
}
guard let safeDistanceFromHome = ParentProfile.safeDistanceFromHome else {
return
}
let bug = true
SwiftyBeaver.debug("The value of bug is: \(bug)")
if bug == true {
SwiftyBeaver.error("There's definitely a bug... Aborting.")
return
}
for location in locations {
let distanceFromHome = location.distance(from: homeLocation)
if distanceFromHome > safeDistanceFromHome {
NotificationCenter.default.post(name: TenPMNotifications.UnsafeDistanceNotification, object: nil)
} else {
NotificationCenter.default.post(name: TenPMNotifications.SafeDistanceNotification, object: nil)
}
}
SwiftyBeaver.info("Got to the end the didUpdateLocations method")
In the sim, set the location to Apple just as you did before. Now build and run the application. Although you’ll have the Xcode console logs available in this scenario, you’re going to ignore them and imagine you’re monitoring cloud logs from a remote user.
Enter a safe distance of 1 km, and press NEXT. After the map loads, change your location to a latitude of: 37.3397 and longitude: -122.0426 via a custom location.
Again, you’ve moved out of your safe zone without the text updating.
You should also notice the following logs repeating on the SwiftyBeaver Crypto Cloud, after setting the log filter to ALL:
Wow, that’s really helpful! If you go back to your code in the LocationTracker
class, you can compare it to the logs and see how far the app got before it stopped executing your code. Here it clearly bailed out in the if bug == true
check, where an error was printed.
To “fix” the bug, simply set the bug variable to false when it is declared in locationManager(_:didUpdateLocations:)
.
let bug = false
Build and run Ten PM. Simulate starting at Apple and moving outside of the safe zone. This time, you’ll see the safe zone warning is properly triggered.
You should also see the following logs in your Crypto Cloud console.
It looks like the app got past the problem and is successfully responding to location changes once more. You squashed that bug!