Don’t use NSLog anymore!

by Josh Highland on April 10, 2010

Screen shot 2010 04 10 at 6.21.16 PM Dont use NSLog anymore!

NSLog is a convenient way to output logging information from your iPhone app. NSLog is most commonly used by developers for debugging purposes. The problem with adding NSLog statements in your code is that you have to remove them or comment them out before you can submit your application to the app store. Apps that output data via NSLog will not be approved into the app store.

I would like to show you how I handle this delema. Start by opening the “_Prefix.pch” file.

Screen shot 2010 04 10 at 8.06.47 PM Dont use NSLog anymore!

next, add the following lines to the file

#define DEBUG_MODE
 
#ifdef DEBUG_MODE
#define DebugLog( s, ... ) NSLog( @"<%@:(%d)> %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#else
#define DebugLog( s, ... )
#endif

In your code, replace “NSLog” with “DebugLog”. THATS IT!

DebugLog adds some new functionality to NSLog. It will now output the file name and line number of the Debug statement.

The best part of this is that its very easy to disable when its time to create a release build of your code. Simple comment out the “#define DEBUG_MODE” line im the “_Prefix.pch” file.

  • digg Dont use NSLog anymore!
  • facebook Dont use NSLog anymore!
  • stumbleupon Dont use NSLog anymore!
  • twitter Dont use NSLog anymore!
  • delicious Dont use NSLog anymore!
  • reddit Dont use NSLog anymore!
  • friendfeed Dont use NSLog anymore!
  • posterous Dont use NSLog anymore!
  • tumblr Dont use NSLog anymore!

{ 3 comments… read them below or add one }

1 Tea September 26, 2010 at 6:31 am

Some of us just get used to the default/standard way of outputting traces, which is probably why even the sluggish ‘System.out.println’ in Java is used in place of easy to design, more elegant replacements.

In this case, you do not need to modify the NSLog semantics just to strip logging off release builds. Here’s a link to a fairly comprehensive article surveying alternatives:

http://iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog

2 david November 4, 2010 at 11:54 am

Regarding App Store acceptance. Is the problem that the NSLog statements are *in* the code, or that the NSLog statements are being *called* in the code?

I have several apps containing NSLog statements in the App Store. Is this a new policy?

3 Tea November 5, 2010 at 10:33 am

I didn’t even know you could get your app removed from the store just for calling NSLog().

Either way, you can leave your NSLog statements ‘in the code’. Do make sure these statements are not ‘being called’ in release mode.

Leave a Comment

Previous post:

Next post: