SwiftyBeaver FileDestination: Logging to a file

In this lesson

In this lesson, I will introduce you to the SwiftyBeaver FileDestination so that you can persist your log statements into a file on your file system.

Transcript

Tap on time to skip ahead

00:11

Hello everyone! In this lesson, I want to talk about the SwiftyBeaver FileDestination. In another lesson, I introduced you to the concept of logging destinations and I ran my application and down here, everything went to the console. I spent most of my time talking about the ConsoleDestination.

00:35

Well, SwiftyBeaver allows you to specify multiple destinations. So down here. I had in the initializeApplication() function, I initialize my logger and I added a ConsoleDestination, which is up here. What I want to do, is introduce you to the FileDestination and we’re going to just have one for now; addFileDestination(). And what I’m going to do is paste in some code that I wrote earlier. And we’re going to take a look at that. If we look at this function here addFileLoggerDestination(). The first thing I’m doing is I’m creating a log folder that will contain the log file that I’m going to create. Then I’m going to create a FileDestination, specify the name in the folder that I created up here. And I’m going to set the URL of the FileDestination to point to this log file that I’m going to use. And then I add that destination to the SwiftyBeaver logger. So it’s as simple as that and if you want to look here at the createLogFolder(), it’s just kind of some boilerplate code for creating a folder in your sandboxed filesystem.

02:03

After running my application again, you’ll notice that not only did all of the log output go to the console, but if we look at Finder, in the sandbox folder of my application and click on this logs folder, you’ll notice that I have a lolChampionBrowser.log file. It’s quite large, 1.3MB for just launching the application. But by adding that FileDestination, not only am I logging to the console but I can specify a secondary log destination which is a file. And I get to choose the name and the location of that as long as it’s within the sandbox of my application.

02:46

If I open terminal, I can use the cat bash command to display the contents of the log. As advertised, SwiftyBeaver does log in color even when you’re using a FileDestination. As long as you don’t override and turn that off. You’ll see in my log I have some WARNING’s and DEBUG’s and various other things. The cool thing about this is that my logs persist to a file and I can take that file and use whatever tool I want to have a look at it.

03:22

In summary, I can add multiple destinations to my logger. As I showed earlier, I had a ConsoleDestination. But in this lesson, I added a FileDestination. The file is a secondary place where all my log messages go and the only thing that worries about that is this code right here. Wherever I’m logging, like down here. This code doesn’t care where the log statements are going. This configuration right here is what controls where my statements go.