I was searching around for a better way to log app data directly on the iPhone during the development process.  I needed something better than the standard NSLog, writing messages to the XCode console.  My situation was that I had a development iPhone 5S but many of my test users were on iPhone 6.  The later OS didn’t seem to be performing in the same way as the 5S and it was difficult to understand exactly what was happening on an ad-hoc build outside of the XCode environment.

Searching around I eventually found two methods that allowed logs to be analyzed directly on test phones.

Method 1 – Logging to a file with NSLog

It’s possible to redirect all your existing NSLog statements to a file on the iPhone rather than the console.  The following function can be added to your app delegate and called from ‘didFinishLaunchingWithOptions’

- (void) redirectConsoleLogToDocumentFolder
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *logPath = [documentsDirectory 
  freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);

Method 2 – SNLog


SNLog is a really handy logging framework developed by Jason Agostoni to provide a little more functionality than the standard NSLog.  It can be added to your project simply by adding two files (SNLog.h and SNLog.m). Initializing the framework is easy as demonstrated below.  (You may want this to be part of your app delegate as your app starts up).

SNFileLogger *logger = [[SNFileLogger alloc] initWithPathAndSize:@"path_to/log.txt"]: 100000];

[[SNLog logManager] addLogStrategy:logger];

[logger release];

Once this is done, the SNLog class can be used all over your project and it will utilize the logging strategy specified (or multiple strategies, as you can provide multiple file destinations plus the console itself).

Syntax for creating a log message can be seen below :

[SNLog Log:@"Test: %@", aString];

You may want to ensure you are logging to the app’s documents folder, in which case the following standard code for finding the documents path and then appending the new log file name can be used when creating the SNFileLogger.

//to log to document directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *loggingPath = [documentsDirectory stringByAppendingPathComponent:@"/mylog.log"];

//or to log to the caches directory (not backed up by iTunes)
NSString *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesPath = [paths objectAtIndex:0];
NSString *loggingPath = [cachesDirectory stringByAppendingPathComponent:@"/mylog.log"];

//redirect NSLog
freopen([loggingPath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);

 You may also find this link interesting if you want to customize/override the functionality in NSLog: