Do I need to disable NSLog before release Application?
One way to do it is to go into your Build settings and under the Debug configuration add a value to "Preprocessor Macros" value like:
DEBUG_MODE=1
Make sure you only do this for the Debug configuration and not for Beta or Release versions. Then in a common header file you can do something like:
#ifdef DEBUG_MODE
#define DLog( s, ... ) NSLog( @"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#else
#define DLog( s, ... )
#endif
Now instead of NSLog
use DLog
everywhere. When testing and debugging, you'll get debug messages. When you're ready to release a beta or final release, all those DLog
lines automatically become empty and nothing gets emitted. This way there's no manual setting of variables or commenting of NSLogs
required. Picking your build target takes care of it.
Suppressing NSLog statements for release?
This is a popular solution:
http://iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog
See comments about using either -DDEBUG=1
or DEBUG=1
.
How do I disable NSLog?
Update:
The answer bellow is actually much better. See here.
Initial answer:
There is a little hack that you could do. Search for all NSLog
and replace them with //NSLog
and than do another search for ////NSLog
and replace them with //NSLog
.
Should I remove NSLogs when releasing my App
I think it is a good practice to not spam the user's device log.
For this, I have a macro, DebugLog
, that is only active for debugging builds:
#ifdef DEBUG
#define DebugLog(fmt, ...) NSLog(fmt, __VA_ARGS__)
#else
#define DebugLog(fmt, ...)
#endif
For all log messages that are interesting to me for development, I use DebugLog
. For all error messages that should be logged I use unconditional NSLog
. This way the distribution builds don't clutter the user's console log. Only important messages get logged.
Disabling NSLog For Production In Swift Project
You'll need to set up a compiler flag to use the Swift preprocessor - go to the Swift Compiler - Custom Flags section of Build Settings to set up a -D DEBUG
flag:
Then in your code you can define a DLog()
function and only print your message if the DEBUG
flag is set:
func DLog(message: String, function: String = #function) {
#if DEBUG
println("\(function): \(message)")
#endif
}
Is it true that one should not use NSLog() on production code?
Preprocessor macros are indeed great for debugging. There's nothing wrong with NSLog(), but it's simple to define your own logging function with better functionality. Here's the one I use, it includes the file name and line number to make it easier to track down log statements.
#define DEBUG_MODE
#ifdef DEBUG_MODE
#define DebugLog( s, ... ) NSLog( @"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#else
#define DebugLog( s, ... )
#endif
I found it easier to put this entire statement in the prefix header rather than its own file. You could, if you wanted, build a more complicated logging system by having DebugLog interact with normal Objective-C objects. For instance, you could have a logging class that writes to its own log file (or database), and includes a 'priority' argument you could set at runtime, so debug messages are not shown in your release version, but error messages are (if you did this you could make DebugLog(), WarningLog(), and so on).
Oh, and keep in mind #define DEBUG_MODE
can be re-used in different places in your application. For example, in my application I use it to disable license key checks and only allow the application to run if it's before a certain date. This lets me distribute a time limited, fully functional beta copy with minimal effort on my part.
How to disable NSLog all over the app?
Xcode has a precompiled header file ({project-name}-Prefix.pch
in the Supporting Files group by default) that is a great place to put code that will be used across every file in the project. For going a step further and improving the log message itself, see Is it true that one should not use NSLog() on production code?.
Related Topics
Determine on Iphone If User Has Enabled Push Notifications
Swift Get Request With Parameters
Dealing With Different iOS Device Resolutions in Spritekit
How to Debug iOS 8 Extensions With Nslog
Adding a View Controller as a Subview in Another View Controller
Changing Root View Controller of a iOS Window
Converting Hex String to Nsdata in Swift
Custom Edit View in Uitableviewcell While Swipe Left. Objective-C or Swift
How to Autoplay a Youtube Video in a Uiwebview
Uiactivityviewcontroller Crashing on iOS 8 Ipads
Outlets Cannot Be Connected to Repeating Content Ios
Uicollectionview - Dynamic Cell Height
Detect If the App Was Launched/Opened from a Push Notification
Uinavigationbar Hide Back Button Text
How to Take a Full Screen Screenshot in Swift