Logging framework: CocoaLumberjack

      No Comments on Logging framework: CocoaLumberjack

In this quick article we'll quickly go over a logging framework that takes your print statements to the next level. We'll be talking about CocoaLumberjack.We all know how useful a simple print statement can be while you’re developing an app. In this quick article we’ll quickly go over a logging framework that takes your print statements to the next level. We’ll be talking about CocoaLumberjack.

A Bit of Info

You use print statements in your daily development. And you know how useful they are. You also know that things can get out of control pretty fast. Just remember how many times you found yourself having to scroll through thousands of lines of console output to find something of interest. Imagine you just checked out code and found that someone on your team added a print statement that was logging a couple of hundred lines of text. And let’s face it, most of the logs we add are status messages (collection prints, checkpoints, etc…)

First thing you’ll love about CocoaLumberjack will probably be the log levels you can use. Not all log messages are equally important, and you can set the log level of the messages you’re logging. So when you’re debugging you can print the most detailed logs, or slightly less detailed logs, it’s up to you.

Another great feature is the log output. You can choose where to send output of your log commands. Obviously, you’ll want to send it to your console, but you can also choose the system logger, or the file logger. Now this is where things get interesting.

I could bore you for another couple of paragraphs, or just tell you what I want to do. I want to log stuff in the console while developing an app, but I also want for my users to be able to send me a log file. And I don’t want everything logged into that log file, just the important stuff.

If you wanted to do this on your own, you’re looking at a couple of days of work, but with CocoaLumberjack it’s a couple of minutes of work. Let’s get started.

A Bit of Code

CocoaLumberjack is available as a pod, it’s the simplest way of integrating it with your project, so go ahead and run ‘pod install’ after adding it to your podfile:

If you’re using swift in your project make sure you import the correct pod.

Setup

You’ll have to import the ‘CocoaLumberjack’ into every file you plan on using it, and you’ll have to set it up before you use it. So let’s import it to our AppDelegate and set it up:

Here we’re adding loggers to CocoaLumberjack using the DDLog.add() method, The first logger we’re adding is our standard console output. The second logger is a file logger. We’re keeping a public variable to our file logger in the AppDelegate (more on this later), and we’re keeping the default settings from the CocoaLumberjack examples. We’ll be storing the last 7 days worth of logs and we’ll create a new file each day. What’s interesting to note here is that we’ll only log messages with the level of ‘.info’ or above. We don’t want to spam our logging file with unnecessary messages.

Usage

If you used print statements, you could pretty much replace all instances with one of the ‘DDLog’ statements, check out these couple of examples:

Everything you log using any of these methods will be logged into the console, but only calls to ‘DDLogInfo’ and higher will be saved into a file.

Sending Logs

A great use case for your logs is for your users to upload the logs to you. You can easily send your logs as an email attachment. Let’s quickly go over an example. Somewhere in your app you’ll have a button that will call a method like this:

After the sanity checks and initialising the mail composer we use the ‘fileLogger’ public variable from the AppDelegate. ‘fileLogger’ has a reference to the file manager that will give you an array of file paths where the logs are saved. We’ll convert these file paths to URLs and create data from it. We’ll save the data in a dictionary where the keys will be the file names.

Next, we’ll iterate over the dictionary and add attachments to our mail. All that’s left is to actually send the mail…

And there you have it, your users sending you logs. When you receive the logs you can see that the logs only contain the necessary data:

How easy was that 🙂

Conclusion

CocoaLumberjack can do so much more, but I believe this will cover most of your use cases. If you’re dealing with a hard to reproduce bug, a good log file is invaluable. This makes it really easy to get the logs off your users and fix production issues.

You can customise your logs beyond belief. You can create custom log formatters, log writers, you can do everything. One of the examples on their GitHub repo is saving the logs in a zip file (I recommend going through that one).

I hope you learned something new today, and that you’ll see a benefit of using a great logging library like CocoaLumberjack. Happy bugfixing and as usual…

Have a nice day 🙂

Dejan.

More resources

0

Leave a Reply