Swift logging destinations: Where do the log messages go?

In this lesson

In this lesson, I will cover the concept of logging destinations so that you can understand where your logged messages go.


Tap on time to skip ahead


Hello everyone! In this lesson, I want to talk about the concept of logging destinations. In some other lessons, you may have recalled where I introduced logging statements throughout my code base. So here’s some lifecycle logging events, the various application lifecycle events in the AppDelegate. And then down here, in the LoLAPIRequestManager, I’m logging HTTP traffic. And down here, I add some extensions to be able to log more intelligently. So there’s all sorts of logging going on and you may have also noticed that when the application ran, it generated this massive log of all sorts of log messages. So how did this get here? Where did it come from? We saw where I have code throughout the application that’s logging, but how did it appear in the console? Well that’s the topic of this lesson.


In all of these logging situations, you’ll notice that I’m logging through this thing called a logger. And in this case, it’s really just a reference to a class. And that’s the way it is and most logging frameworks. So you will see here I’m logging through this logger component and really the longer is just a conduit through which you log. Where the log statements appear is another matter.


So let me introduce you to the concept of logging destinations. In the applicationDidFinishLaunchingWithOptions lifecycle function here, I call this initializeApplication function. If we look at that, the first line initializes the logger. So if we look at this right here. You’ll notice that I have two functions that I call; addConsoleLoggerDestination() and addFileLoggerDestination(). Let’s take a look at the console And what I’m doing is constructing a new destination and I’m making some settings on it. And then I’m adding that destination to the logger. Since the consoleDestination logs statements to the console down here. By adding that destination to the logger, that’s what causes these log statements to appear in the log. If I didn’t want that, I certainly then would not create and add a consoleDestination to my logger. And what that means is that if I don’t add that, then nothing will appear in the console when I log in my application.


And most logging frameworks have a console logger. But you also may have noticed is that I had an addFileLoggerDestination() down here. So let’s go look at that. That’s another function in my application and I create a different type of destination. I configure it. And this requires a little bit more configuration because it not only wants levels and things like that but it wants to know where I want to put my log file. So I go through that process and then down here at the bottom on line 99, I add the file destination. So what this is going to result in is, not only will all my log statements that I’m making throughout my application like up here, go to the console, but they’ll also go to this file. So if I didn’t add the file destination, obviously my log statements would only go to the console.


After running my application, I’ve placed the log file in the /Documents/logs folder of my app and there it is. It wrote a lolChampionBrowser.log file. And you’ll notice it’s quite large. Just by running the startup process it’s 1.3 megabytes. Well you can obviously filter that if you want but the point being, I added a file destination to my logger which resulted in the log statements being written to a file on my device filesystem. If I double-click on the log file, it will open up the OS X log viewer. And not surprisingly, all of my log content has been written to a log file.


In summary. The logging destinations allow you as the developer to control where your log content goes. Here we have the console and here we have the file. You can even create your own, depending on what logging framework and customize your own logging destinations. 

Additional Info

Register to get access to additional resources and info.