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.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

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!

Where to Go From Here?

You can find the completed sample project from this tutorial here.

Also check out File Logging with SwiftyBeaver. If you have an iPhone and want to be 100% certain that you’ll get your logs (even when there’s no Internet connection), this feature can be incredibly helpful.

For more advanced use cases, check out custom formatting for your logs.

If you have any questions or comments on this tutorial or SwiftyBeaver, please come join the discussion below!

Ted Bendixson

Contributors

Ted Bendixson

Author

Jeff Rames

Tech Editor

Chris Belanger

Editor

Andy Obusek

Final Pass Editor and Team Lead

Over 300 content creators. Join our team.