SwiftyBeaver setup: Configuring in your AppDelegate

In this lesson

In this lesson, I will teach you how to do some basic SwiftyBeaver setup in your AppDelegate and immediately start logging.

Transcript

Tap on time to skip ahead

00:11

Hello everyone! In this lesson, I’m going to talk about the SwiftyBeaver logging class, how to integrate it into your application and your dynamic frameworks so that you can begin logging in your apps and frameworks.

00:30

I’m here in the AppDelegate and I want to start integrating logging in my application. I’m going to import the SwiftyBeaver framework which I’ve already pulled down using Carthage or Cocoapods are you can use whatever you’d like to get it. And then I have two choices. I’m going to either creat an alias here called logger and point it to the class SwiftyBeaver. And what this does, is inside of the AppDelegate, I can then log through this thing called the logger. The entire SwiftyBeaver API is static meaning that I do not create an instance of SwiftyBeaver I would not do something like this. Everything is done through the class itself. So, I can either do this… I’ve also seen some references to a preference to make it a global property so that you don’t have to do this in every class. I’m not sure how I feel whether I like the global approach better or. I’ve tended to declare this in every single class that I want to do logging, but I’ll leave that up to you, whatever you prefer. This line in pretty much every class that you want to log in or you can declare a global variable and reference that anywhere without including this line.

02:07

Now that we have are logger defined here online 18, I can start logging. It’s as easy as that. So I’m going to log all my lifecycle events. I’ll describe this here in a second, but I’m going to log at the verbose logging level. And I’m going to create a prefix here so I can later use this for something else called filtering. But I’m going to say the applicationWillResignActive. And then up here I need to add one is well..logger.verbose lifecycle. “The applicationDidFinishLaunchingWithOptions. So like those cooking shows, I’m going to repeat this for all of the lifecycle events and I’ll come back when I’m done.

03:13

I’m back. And as you can see in all of the lifecycle AppDelegate functions; applicationDidFinishLaunchingWithOptions, I’ve added a logging statement at their verbose logging level, and is I mentioned I’ll cover this in more detail later. ApplicationWillResignActive, applicationDidEnterBackground, applicationWillEnterForeground, applicationDidBecomeActive and appolicationWillTerminate. So I’ve added logging to each of those and I have one more place where I’d like to take a look. Down here in the syncDataDragon() function of the AppDelegate. Look what I have. This is a no-no. Without logging, this is probably what you’re doing and this is what logging is intended to replace. So we want to get rid of this. This is not something we want because we don’t want this to print when we actually submit our app to the AppStore. And most likely if you do this, you’re putting comments and commenting out that code all over the place in your application. And when you want to turn that on, you’re doing something like that. We don’t want to do that.

04:28

Instead, I want to use our logger. So I might as well use logger. And I’m going to do all of this at a different logging level and I’m going to use .INFO. And that is a higher logging level. What that means is that it’s more important So self.logger.info(“DataDragon sync completed”). So by doing it this way, I don’t have to comment this out and I’ll show you all about this in another lesson. The point is, I have logging now inside of my AppDelegate using the SwiftyBeaver logging framework and I’m logging at two different criticalities or logging levels; .Verbose here and .Info here.

05:22

So I’ve launched my application with the logging in both the AppDelegate and in some of the dynamic frameworks that I depend on for this application. You can see the application running over here, but what the heck? Where’s all my logged statements? Here’s this one, clearly these functions executed. And I would expect in the console at least, to see statements that I logged. Where are they? Well, you’re gonna have to stay tuned for another lesson because I’ll tell you all about that in the next lesson. 

Additional Info

Register to get access to additional resources and info.