This is a better way to use NSLog. Adding the next snipped of code in your app pch file (app-name-Prefix.pch) and then replacing all the “NSLog” of your project for “DebugLog” we can improve the performance of the app when it is not in the debug mode.
Another feature is that this code also shows the File name, line number and method name for each log. I added some more stuff so all the logs appears aligned by columns and are easy to read.
1 .Add this code in your pch file.
// Shows the logs just in the debug mode with the fileName, line number and method.
#define DebugLog( s, ... ) NSLog( @"[FILE]%@ %*s [LINE]%-*d [METHOD]%@ %*s [MESSAGE]%@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent],30 - ([[[NSString stringWithUTF8String:__FILE__] lastPathComponent] length]),"", 5,__LINE__, NSStringFromSelector(_cmd), 75 - ([NSStringFromSelector(_cmd) length]),"", [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#define DebugLog( s, ... )
2. Replace the NSLog for DebugLog.
NSLog(@"Log with some message");
DebugLog(@"Log with some message");
// Results Before:
Log with some message
// Results After:
[FILE]ViewController.m [LINE]30 [METHOD]viewDidLoad [MESSAGE]Log with some message