Handle swift logs and testing safely and painless

If you feel like Buzz when you’re developing an app you should read this post

If you started developing iOS or OS X apps with Swift maybe the word DDLog doesn’t ring a bell but don’t worry, you just need to know that DDLog can be used instead of NSLog (among other things) if you don’t want your logs to be readable in release.

Releasing an app with logs is really not recommended for many reasons such as:

  • NSLog slows down the app because of its synchronism
  • network requests are usually logged and you probably don’t want other people to know how your backend works

Another thing that you’ll have definitely notice is that you don’t need all the logs every time, indeed often the debug process is structured like this:

  1. build the app
  2. landing on the view (or whatever) you need to test
  3. clear the log console
  4. do your test and see the logs

A simple solution is to use colored logs, but once you have used the 7 or 8 colors you know, you have to re-use the same colors for different logs; if you’re are a girl who knows more than 8 colors you can go for it (maybe it’s for this that women are better than men at coding? :D) but at least I, need to figure out a better way.

So let’s recap the post until now

  • we need logs only when they are necessary
  • we don’t want logs in release
How we can efficiently solve this problem? With enums of course.


Let’s imagine we have a list of kittens that we retrieve from our backend and want to show it in our TableView.

We want to log

  • the network request and response
  • the actions about the view (display a new cell, tap on a cell, ecc..)

and we don’t want to manually navigate our app until the view controller who contains the TableView (yes, we’re lazy).


Here’s the code

Basically we have a case for each type of log so we can turn on and off them individually using the switch inside the function isEnabled and the #if DEBUG prevent any case for being enabled in release.

You may have notice that the enum name is Test and not something like Log. That’s because:

  • I find it more understandable reading something like Test.kittensNetworkService.stuff()
  • This solution is not limited just for logs: remeber that we don’t to manually navigate until the kittens VC?


We want to log the indexPath of the selected cell?

We want to navigate until the KittensVC?

If we need to do something in any of a list of case we can use the performBlockFor method


To use the #if DEBUG macro we have to add the value -D DEBUG in the Build phases of the target / Swift Compiler — Custom Flags / Other swift Flags”

Thanks for reading until here, I hope you’ll like this solution as much as I do :)