7

I have read this post: what happens to NSLog info when running on a device?

...but wondering if NSLog is a problem when distributing the app such as filling up the memory or something? I am only interested to see it when i test the consistency of my input data to the database.

The reason is that I have NSLog to control when i load the data into my database in the simulator. I could remove it when i upload but it would be good if i do not need to?

Community
  • 1
  • 1
PeterK
  • 4,243
  • 4
  • 44
  • 74

5 Answers5

7

You should remove it. For example if you log contents of a UITableViewCell (in -tableView:cellForRowAtIndexPath:), it can make a big difference in performance, especially on slower hardware.

Use a macro to keep NSLog output in Debug mode, but remove it from Release mode. An example can be found on the following site: http://iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog

Wolfgang Schreurs
  • 11,779
  • 7
  • 51
  • 92
5

I use a set of macros in my pch file that are quite handy for this.

See http://www.cimgf.com/2010/05/02/my-current-prefix-pch-file/ for details.

#ifdef DEBUG
  #define DLog(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])
  #define ALog(...) [[NSAssertionHandler currentHandler] handleFailureInFunction:[NSString stringWithCString:__PRETTY_FUNCTION__ encoding:NSUTF8StringEncoding] file:[NSString stringWithCString:__FILE__ encoding:NSUTF8StringEncoding] lineNumber:__LINE__ description:__VA_ARGS__]
#else
  #define DLog(...) do { } while (0)
  #ifndef NS_BLOCK_ASSERTIONS
    #define NS_BLOCK_ASSERTIONS
  #endif
  #define ALog(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])
#endif

#define ZAssert(condition, ...) do { if (!(condition)) { ALog(__VA_ARGS__); }} while(0)
Abizern
  • 146,289
  • 39
  • 203
  • 257
3

The log on the device only keeps around an hour of data, even Apple's apps log quite a few things there. So it is generally acceptable to have meaningful output in the log.

But since logging is a disk operation you might find that excessive logging slows down your app since the writing blocks the main (= UI) thread for a short while for every NSLog.

Because of this you should only log things that give more information if errors are happening in your app to facilitate finding what is going wrong.

Cocoanetics
  • 8,171
  • 2
  • 30
  • 57
1

Consider switching to cocoalumberjack, it is a drop-in replacement for NSLog and has significantly more useful functionality and better performance.

It is similar in concept to other popular logging frameworks such as log4j, yet is designed specifically for objective-c, and takes advantage of features such as multi-threading, grand central dispatch (if available), lockless atomic operations, and the dynamic nature of the objective-c runtime.

Smilin Brian
  • 980
  • 8
  • 18
Aidan Steele
  • 10,999
  • 6
  • 38
  • 59
0

There only real reason to remove your NSLog entries is to save on memory consumption by running unnecessary code, but unless you're consistenly adding data, it shouldn't be too much of an issue. Furthermore, if a user has an issue such as app crash, etc. NSlogs can be submitted which the dev can read to work out the reason for the crash. If you're loading the NSlog with a great deal of unnecessary data, it can be troublesome at a later date if you need to go through said log to find a user's issue in order to fix the issue.

Ultimately, I wouldn't worry too much about removing them. That's my 2 cents anyhow.

JapanDev
  • 341
  • 2
  • 12
  • I generally agree with this, although in situations where 100% performance is required (i.e., looping through a large collection) then you probably want to remove the NSlogs as there is a performance hit when logging. In general situation you won't notice it though. – sosborn Jun 20 '11 at 05:06