Swift logging strategies: Log all the things!

In this lesson

In this lesson, I’ll teach you some easy-to-follow Swift logging strategies so that you can learn what to log in your Swift apps.


Tap on time to skip ahead


Hello everyone. In this lesson,, I want to talk about the single most important thing about logging in your application. That is you need to learn to log everything. You’ll notice here, that the application initially logged when it was initialized. And if I scroll to the very bottom, there’s all kinds of things going on. There’s probably thousands of lines of log statements in this log, ending with the fact that it completed a sync event.


So everything that occurred between the point of initialization and this nearly last line in the log is logged at great detail throughout the application. So we’re going to take a look at where I do that and what it looks like.


Let’s see if I can provide a little method to what appears to be a bunch of madness with all those log statements. The first thing we want to do is we want to log events. Events could be lifecycle events, like all of these AppDelegate events; applicationDidFinishLaunching, willResignActive, didEnterBackground etc. And/or they could be button taps or taps on table cells. So what I’m going to do is add logging to each of these events. And I’ll come back and show you when I’m done.


I’ve completed adding the logging statements to each of the life cycle events for the AppDelegate. So here is didFinishLaunchingWithOptions, willResignActive, didEnterForeground etc. If I scroll down here, I also perform an action, syncing data dragon. I want to add logging to that. So here on the completion side, when it finishes, I already had this but I changed the wording a little. When it launches or starts, let’s go look at that code. Skipping all of this for a minute, notice that I added a data dragon sync started. Let’s skip the error handling for now, but the point of this is that I’m logging events. Those are things that I start, various life cycle events, when they complete etc. So get in the habit of logging all events, start and completion.


The next thing we want to focus on logging is, all activity in our application. For this application that’s primarily network and SQL database activity. So I’m using Alamofire here. And I am able to capture all outgoing network requests by overriding AlamoFire’s request function. And notice here that I’m logging the outbound network requests and I’m logging the method, the URL, whatever parameters are passed, the encoding and the headers. So that’s the outbound side.


To log the end of the response side of the network activity, we’re going to look at a couple of my commands that issue these requests. So you’ll notice here that I am in the CacheChampionImagesCommand. I am requesting via Alamofire, a URL and on the response handler side, I’m logging the response. The same holds true for the GetChampionDataCommand. I’m using the request, Alamofire request function and in the response JSON handler, I’m logging the response. Same for the GetRealmCommand. So, I’m logging both the outbound request and then on the response side, I’m logging the response.


In terms of database activity, I’m going to scroll down here. And I have two main classes in my application that perform SQL. Here, the SQLQueryOperation invokes all SQL SELECT statements. So depending upon how I’m invoking it, you’ll notice here that I’m logging either the statement withNamedParameters or I’m logging the statement withParameters, a different way of calling. The SQLUpdateOperation is where all of my INSERTS, UPDATES and DELETES are invoked. And similarly I’m logging the DELETE here, I’m logging the INSERT here and I’m logging the UPDATE here.


The key point, log all network and database activity and whatever activity makes sense to your application. The last thing we want to do is log all errors or warnings. So here in the Actions.swift, I am performing something that could result in an error and you’ll notice here that I’m logging an error. Down here, same thing. I am performing an action that can result in an error and I’m logging the error. And finally, the same here.


So get in the habit of logging all errors when they occur. That way you can see exactly what’s happened if something occurs that you don’t expect.