Logging from iOS to logentries.com

Logentries.com is such a great service for your logs.
If you wish to report your iOS application crashes into your logentries.com account, read on!

Download PLCrashReporter 1.1 binaries

Open the downloaded DMG and copy iPhone Framework/CrashReporter.framework to your project folder

Open your project in XCode and drag’n’drop the CrashReporter.framework folder under Frameworks group:

Confirm:

The result should look like this:

Add the following import into your application delegate implementation:

[objc]
#import
[/objc]

Add the following constants (make sure that the Host specified exits in your logentries.com account):

[objc]
#define LOGENTRIES_API_KEY @”
#define LOGENTRIES_HOST_KEY @”LogEntriesReportingTest”
#define LOGENTRIES_LOG_KEY @”iOS.log”
[/objc]

Add the following code at the beginning of your application:didFinishLaunchingWithOptions: method:

[objc]
//set up crash reporter
PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter];
NSError *error;

// Check if we previously crashed
if ([crashReporter hasPendingCrashReport]) {
[self handleCrashReport];
}

// Enable the Crash Reporter
if (![crashReporter enableCrashReporterAndReturnError: &error]) {
NSLog(@”Warning: Could not enable crash reporter: %@”, error);
}
[/objc]

Add the following method:

[objc]
– (void)handleCrashReport {
NSLog(@”App has crashed on last run, loading crash report…”);

PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter];
NSData *crashData;
NSError *error;

// Try loading the crash report
crashData = [crashReporter loadPendingCrashReportDataAndReturnError: &error];
if (crashData == nil) {
NSLog(@”Could not load crash report: %@”, error);
[crashReporter purgePendingCrashReport];
return;
}

// We could send the report from here, but we’ll just print out
// some debugging info instead
PLCrashReport *report = [[PLCrashReport alloc] initWithData: crashData error: &error] ;
if (report == nil) {
NSLog(@”Could not parse crash report”);
[crashReporter purgePendingCrashReport];
return;
}

// convert crash report to human readable text (which can be converted then to symbolicated output with symbolicatecrash script
// and app.dSYM file)
NSString *humanReadable = [PLCrashReportTextFormatter stringValueForCrashReport:report withTextFormat:PLCrashReportTextFormatiOS];

// prepare request to send crash report to logentries.com
NSString *logEntriesURL = [NSString stringWithFormat:@”http://api.logentries.com/%@/hosts/%@/%@?realtime=1″, LOGENTRIES_API_KEY,
LOGENTRIES_HOST_KEY, LOGENTRIES_LOG_KEY];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:logEntriesURL]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:20.0];

[request setHTTPMethod:@”PUT”];
[request setValue:@”text/plain” forHTTPHeaderField:@”Content-type”];
[request setValue:[NSString stringWithFormat:@”%d”, [humanReadable length]] forHTTPHeaderField:@”Content-length”];
[request setHTTPBody:[humanReadable dataUsingEncoding:NSUTF8StringEncoding]];

// send request
NSLog(@”Sending crash report to %@”, logEntriesURL);
[NSURLConnection connectionWithRequest:request
delegate:self];

// Purge the report
[crashReporter purgePendingCrashReport];
return;
}
[/objc]

You should be able to launch your application on a device.

Now add some error somewhere (i.e. into a button tap handler):

[objc]
[NSException raise:@”Test Exception” format:@”Crash occurred on %@”, [NSDate date]];
[/objc]

Launch the app on your device and click the button. It should crash.

Now launch the app again on your device so that the crash report is sent to logentries.com.

Check logentries.com:

If you want to symbolicate the crash (to see on which line your app crashed), download the log from logentries.com (in a PLAIN text format).

This will be discussed in the next article.

You can download the sample project here (for XCode 4.5). Make sure you put in your logentries.com API KEY.

Comments are closed.