Logger: A lightweight logging framework

Over the years I’ve been writing and maintaining iOS apps, there’s always been something missing that could’ve helped me fix the bug, or track it down, so much faster. So the other day I decided to do something about that, and create a lightweight logging framework written in Swift to alleviate these pains.

Logger is a framework that you set up in just a couple lines, and lets you, your QA team, or UAT testers provide you with easy to read log files that you can use to debug your app. You can try it out by downloading the code and adding it as a new target in your app.

Now to set it up, just head to your app delegate and add the following couple of line:

Screen Shot 2018-03-23 at 11.54.38.png

Notice the compiler flags, where dev will both print log statements to the console, and write to a file, but any other build will just write to a file. The Logger.canBeDisplayed boolean is for apps where perhaps the data is sensitive and there are certain times you don’t want logs to be displayed, like before anyone has logged into the app. This will prevent the Logger UI from being displayed if set to false.

Logger supports sharing logs from the built-in UI, but if you want to share them through iTunes too, go into your info.plist and add this:

Screen Shot 2018-03-23 at 11.58.20

However, if your app has sensitive data it logs, you might not want this, and instead might want to change the canBeDisplayed property of Logger.

Now the last thing we need is a way to actually display the UI. I prefer to do this when the user shakes their device, as this isn’t usually something that is used for anything else. If you want to do this, you can just grab the code below and paste it into a file in your app.

Now the last thing to do is actually log stuff! Now import Logger, and anywhere you’ve got print, you can just change the word print to log and it’ll work! And if you want meta data about the call site, like function name, file name, line number, you can pass a second parameter to log() of level, and specify the log level.

log(“This will log”)

log(“This is log and give meta data”, level: .debug)

Now run the app, shake your phone, and see for yourself!

ezgif-1-b0c3f85a8a.gif

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s