There has been an avalanche of Swift based logging frameworks lately. After trying several, I found SwiftyBeaver was both flexible and simple enough to move all of my apps onto. Similar to most of the other logging frameworks, SwifyBeaver is extensible. But, unlike many others, there is no real magic injected into the framework making it easy to read and hopefully maintain. This is best demonstrated by how compacted it’s plugin system is. You really only need to write you business specific information, the rest is handled for you.
I use Google Analytics for behavior tracking and basic exception tracking today. Although all of my Google Analytics code is centralized exception reporting is handled explicitly. With the move to SwiftyBeaver I wanted to see how reporting exceptions to Google Analytics could be handled automatically as part of my logging strategy. To accomplish this I created the SwiftyBeaver Google Analytics Exception Logger, available here. Just as the very long name indicates this plugin will automatically post error information to Google Analytics.
Before you start
After both SwiftyBeaver and Google Analytics have been installed you need to copy the plugin into your project. Instructions for doing this are available here. You’re now ready to start configuring logging in your project.
Creating your Logger
Creating a logger in your project is extremely simple. Typically you will want to add the below to the top of your AppDelegate.swift so you can use logging throughout your project.
import SwiftyBeaver let logger = SwiftyBeaver.self
Adding the Google Analytics Logger Destination
Now that you have created your SwiftyBeaver logger you need to add destinations. Without adding any destinations SwiftyBeaver wont actually do anything. For this example I’m going to add two destinations. The first will be the built-in Console destination which simply writes to the Xcode console.
let console = ConsoleDestination() logger.addDestination(console)
Next we’ll add the Google Analytics Exception Logger. When creating the GABeaver plugin you must added your Google Analytics key. This will be used when reporting Exceptions. You can also specify the reporting threshold. This parameter controls the minimum logging level that should be reported to Google Analytics as an exception. By default this is set to only report error levels or greater. If you wanted to report warnings or higher you could simply provide a threshold of warning and the plugin will automatically send both warnings and errors.
Below illustrates how to add the Google Analytics Exception plugin with the default settings.
let gaErrorLogger = GABeaver(googleAnalyticsKey: &quot;your GA Key&quot;) logger.addDestination(gaErrorLogger)
By default only Error Log messages will be sent to Google Analytics. You can change this by setting the Threshold property on the logger.
For example, you can record all message with a level of WARNING or great by doing the following:
gaErrorLogger.Threshold = SwiftyBeaver.Level.Warning
You can also configure the logger to write to the console each time a message is logged. To enable console logging, set the printToConsole property to true as shown below:
gaErrorLogger.printToConsole = true
Additional information is available on github at SwiftyBeaver-GA-Exception-Logger.