SwiftyBeaver destinations: Where do logged statements go?

In this lesson

In this lesson, I will introduce you to the 3 SwiftyBeaver destinations to control where your log messages appear.

4:34 Swift 200


Tap on time to skip ahead


Hello everyone! In this lesson, I’m going to talk about how to configure destinations so that the statements that you log appear somewhere. In a previous lesson, I showed you how I added the logger to my AppDelegate and added logging statements at various log levels in my AppDelegate. I even ran my application over here, but nothing appeared in the console. Minimally, I would expect something to appear in the console.


So I want to introduce you to the concept of destinations. Just like in life, if you have no destination, you’re not going anywhere. Well in SwiftyBeaver, without any destinations, you won’t have any of your statements logged. So let me address that. I’m going to add an initializeLogger() function here. And let’s go ahead and write that of course. Private func initializeLogger(). And what we’re going to do is addConsoleDestination(). I’m going to write a function called addConsoleDestination(). Make that another private function. And of course we’re just going to say let consoleDestination = ConsoleDestination(). That’s a type in SwiftyBeaver. I’m going to add a configuration option here. We’re going to ignore this for now but, I’m going to say colored = false for now. I’ll cover this in much greater detail in another lesson. And then I’m simply going to add to the logger addDestination(consoleDestination). I now have a destination and I would expect since it’s called the ConsoleDestination that all my log statements will appear in the console.


So let’s run the application again and see if that’s true. Now that I’ve added a single ConsoleDestination, let’s go ahead and build and launch the application. And we’re going to see if anything looks different with the ConsoleDestination than it did. So here the application is launching. And all of a sudden I’m getting all kinds of output in my console, if you look down here. So, J=just by simply adding a ConsoleDestination to SwiftyBeaver, it directs all of my log statements to the console.


In summary here, we need to have at least one destination configured for SwiftyBeaver in order to direct our logged content or log messages somewhere. If we look at the console log, you’ll notice one thing I wanted to point out here, applicationDidBecomeActive. Well had a lifecycle logging message here for applicationDidFinishLaunchingWithOptions, which should have run before this. The problem is I logged before I initialized my logger. So my destination was not added until after that log statement was printed or generated. In order to fix that, all I have to do is move this down one line after initializeApplication() and that will be fixed. And for reference, SwiftyBeaver has other logging destinations that we’re going to talk about in other lessons. Namely, the FileDestination and SwiftyBeaver platform destinatin. 

Additional Info

Register to get access to additional resources and info.