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 2 of 3 of this article. Click here to view the first page.

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:

  1. Verbose: The lowest priority level. Use this one for contextual information.
  2. Debug: Use this level for printing variables and results that will help you fix a bug or solve a problem.
  3. 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.
  4. Warning: Use this log level when reaching a condition that won’t necessarily cause a problem but strongly leads the app in that direction.
  5. 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.”

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.

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.

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.

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.