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:


The result should look like this:

Add the following import into your application delegate implementation:


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

#define LOGENTRIES_HOST_KEY @”LogEntriesReportingTest”
#define LOGENTRIES_LOG_KEY @”iOS.log”

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

//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);

Add the following method:

– (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];

// 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];

// 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,

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:logEntriesURL]

[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

// Purge the report
[crashReporter purgePendingCrashReport];

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

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

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

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.