Custom Logging in Obj-C

If you’ve used Objective-C for Mac or iOS development you’re probably familiar with NSLog(); to log to the console.

Well that is fantastic for development, but you don’t really want to output your debugging logs on distributed iOS apps do you?

It would be a real pain to remove every instance of NSLog from your code everytime you wish to distribute your app.  Well you can solve this with a custom logger.

In your Prefix.pch file add the following bit of code

#ifdef DEBUG
#  define RHLog(...) NSLog(__VA_ARGS__)
#  define RHLog(...) ;

Then whenever DEBUG is set (either by adding DEBUG to your pre-processor macros, or using the -DDEBUG flag) instead of calling

NSLog(@"String: %@", stringValue);

you can call

RHLog(@"String: %@", stringValue);

And when in debug mode the string will log to the console, in Release, Ad-Hoc and Distribution builds it won’t

However you can go one further than this and actually produce a much nicer logging function using

#ifdef DEBUG
#   define RHLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#   define RHLog(...)

and when you call your function like so


You’ll see the following in your console

-[AppDelegate startup] [Line 123] Testing

* Image Credit Fryhstyxei

Leave a Reply