Swift logging: Every app needs logging

In this lesson

In this lesson, I will talk about the need for logging in every single Swift app you write, regardless of how big or small.


Tap on time to skip ahead


Hello everyone. I’m Jeff and welcome to the Swift Logging Tutorial. Before we get started, I want to ask you a simple question. When you write your applications, are you logging in your applications? If the answer is no, stay tuned because we’re going to learn all about logging and how important it is to have in every single application that you write. 


To illustrate what I mean I’m going to launch an application that I’ve written. And it’s an application that currently has no logging enabled. At least to speak of. And when I mean logging, I mean what appears down here in the console when your application is running. So if you see the application here has launched in the simulator, and it’s running and there’s absolutely nothing going on over here in the console aside from this simple message that gets printed which will take a look at here in a second. 


There’s actually a lot going on here. You can see that it has retrieved data from somewhere that it displays in the simulator. There’s all these images having to do with champions, there’s champion names. And if I click on any of the champions you’ll see that there’s some other data. There’s obviously a lot going on here but you can’t tell by looking at the console.


So I’m going to contrast that with a version of this application that does all sorts of logging. I’m going to launch the application once again. But, you’re going to notice a drastic difference. That is, that I have enabled logging in this application. If we look down here in the console, the application02 is now going to give all sorts of information about what’s going on. And, the point of this series is to show you how important all of this data really is to you as a developer, how easy it is for you to incorporate it in your applications, and why you want to start logging today.


Logging is how your application tells the story of what it’s doing. If we look at this log, we can start at the beginning and we can see here that the application reported that it was initialized. And then immediately, it started reporting every SQL statement that it executes. The first one was a SELECT. And then down here we can see some others, some other SELECT’s, some DELETE’s, some INSERT’s, etc. And then way down here toward the bottom, let me scroll this log down near the bottom, you’ll notice that it starts caching images. And it’s reporting every single image that it’s caching. And what I want to illustrate here is that some of these images actually weren’t found. And without logging, I would not have known this. This is an example right here. I got a 404 trying to cache this image. And because I have a logging turned on and at a very high level, every single thing the application is doing is reported to the log. And let me finally scroll down to the bottom here. When it eventually completes, it reports how long it took, 76 seconds to cache 1,644 images. And that the the sync process completed.


So the point of this is, logging tells the story of what your application is doing while it’s running, even though it may not be evident when you’re watching the application run.