NSLOG

Ödeal
Odeal-Tech
Published in
3 min readApr 6, 2016

I firmly believe that you are only as good as your tools and as a result, I am always on the look out for new and shinny ones. But recently, I decided to sharpen what I already have, starting with XCode, terminal and ObjectiveC itself.

Macro

One of the tools I use far too much and never thinking about is `NSLog`. Early on, I took a good advice and replaced `NSLog` with my own macro and never looked back. This was a common approachand it served me well over the years by growing and evolving but I deciced that I deserve a better tool so I went hunting on github

ExtendedNSLog

If you want a simple replacement and want colors to distinguish different blocks of log (or espacialy bored wth console) you might like this one.

The idea is replacing `NSLog` macro with a method, wich parse log message and displays it in different colors according to the prefix found. For example


NSLog(@”#! this an recoverable error!”);
NSLog(@”#? this an a 4 minute warning”);
NSLog(@”#. this an a regular joe log message”);

will print a red on black error on your console, followed by a yellow warning and a standart log message. Assigning these to a few snippets should improve your logging behavior and help console readibility.

Alos,it does not try to replace `NSLog` calls, which is a good thing for existing projects.

Note: As documentation states, you have to include the Plugin XCodeColors

CocaLumberJack

This is a powerful tool from a very productive guy, who also is responsible for Xcodecolors which ExtendedNSLog relies on.

This is logging framework which also have the ability to write its output in console. It has many features which are already present in http://logging.apache.org/log4j/2.x/ for other languages.

Contrary to `NSLog`, Lumberjack gives you four different methods to choose from, depending the severity of the message

You also have the ability the decide which are those methods will log in release environment, which is consistently overlooked as projects grow larger.


#ifdef DEBUG
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#else
static const int ddLogLevel = LOG_LEVEL_WARN;
#endif

One of the major features in lumberjack is the ability to log into a file and custom formatters. For most the apps, this would not make sense other than taking extra space but this particular feature comes real handy when developing our mobile payment app By combining these two features, we created a custom formatter which encrypt log messages into a log file, while client is making a payment. Upon query, the server might ask application to send this encrypted log file which serves yet another security check for us.

NSLogger

NSlogger is much more network oriented logging project. Your application dumps it logs into network using Bonjour framwork and another Desktop application recives and interprets them. As documentation notes, this is more of a development and debugging tool then a NSLog replacement. You will also need to build&run custom made logviewer desktop application.

Instead of using XCode console, clients send their messages over the network and logviewer app interprets them. This gives NSLogger a huge advantange over other platforms for it is also able to show images and filtering/sorting them.

This project saved us lots of time when dealing with multiple apps messaging/synchronizing whith each other. Instead of going back and forth between machines/projects console, we end up dumping logs to this from both apps.

notes:
* NSLog is (stil) slowing down applications and most of it should absolutely removed from relase build.
* I know those are not exactly `NSLog` replacement but rahter different logging approches and they are nice tools anyway.
* I try as much as possible the temptation to dump an object value with `NSLog` and tried to use `lldb` commands. Once I get used to it, you gets free from log-build-run-see cycle.

— anil

--

--