47

I found a way to log custom caught exceptions in the Crashlytics Android SDK, but I can't find anything like that for the iOS SDK. Is there a way to log a caught exception with Crashlytics on iOS?

See Android explanation: http://support.crashlytics.com/knowledgebase/articles/202805-logging-caught-exceptions

Sinu Varghese
  • 800
  • 1
  • 14
  • 39
Magnus
  • 1,174
  • 2
  • 11
  • 21
  • This would be useful. The only alternative I see right now (to get the crash report/stack trace) is to actually cause the app to crash - using [[Crashlytics sharedInstance] crash]; which obviously is not an ideal user experience. Most application errors detected within code can be more gracefully recovered from than having the app crash, but it's still really useful to have breadcrumbs and stack trace at that point. – Bradley Thomas Mar 20 '15 at 13:39
  • While this is being implemented, you can do a `CLS_LOG` (see [docs](http://support.crashlytics.com/knowledgebase/articles/92519-how-do-i-use-logging)) to log certain messages, they will be added to the next crash. – vrwim May 12 '15 at 14:18
  • Do you know if it has it been implemented yet ? – Shobhit Puri Aug 07 '15 at 19:45
  • Leveraging `recordError` I wrote my own global `recordException` for try/catch blocks that I know will not be fatal. – Jacksonkr Aug 28 '17 at 16:17

5 Answers5

43

Mike from Crashlytics and Fabric here.

You can now capture logged NSErrors in your iOS, tvOS, or OS X app. You want to use:

[CrashlyticsKit recordError:error];

or

Crashlytics.sharedInstance().recordError(error)

This will let you capture a fair number of logged NSErrors per user session. These are only sent on app relaunch. Logged errors errors are grouped by the error domain and code. This means error issues can span many different call sites.

See Documentation

Ali Saeed
  • 1,519
  • 1
  • 16
  • 23
Mike Bonnell
  • 16,181
  • 3
  • 61
  • 77
  • 1
    What if I want to log a caught NSException, not a NSError? – davis Mar 31 '16 at 14:48
  • 3
    @davis We don’t offer a facility for logging or recording NSException instances directly. Generally speaking, the Cocoa and Cocoa Touch APIs are not exception-safe. That means the use of "@catch" can have very serious unintended side-effects in your process, even when used with extreme care. We recommend that you never use "@catch" statements in your code. From Apple docs: "The Cocoa frameworks are generally not exception-safe. The general pattern is that exceptions are reserved for programmer error only, and the program catching such an exception should quit soon afterwards." – Mike Bonnell Mar 31 '16 at 15:08
  • It seems that on Fabric website a can only see the value under NSLocalizedDescriptionKey, but there might be other helpful information under other keys. I can generate my own error and put everything to NSLocalizedDescription but it.. smells – Oleksii Nezhyborets Aug 17 '16 at 20:08
  • Found a topic for my own question above: https://twittercommunity.com/t/crashlytics-non-fatal-issues-extended-information/64756 – Oleksii Nezhyborets Aug 18 '16 at 12:44
  • 2
    @MikeBonnell What if the user does not open the app again ? All the locally logged NSErrors will never be pushed to crashlytics servers ? If so, and those locally logged errors are important for resolving problems, should NSErrors be logged as Answer events ? Please clarify. – geekoraul May 24 '17 at 00:23
  • 1
    That's correct. If the user never relaunches the app, then the NSErrors would not be sent. For most apps, we've found this to not be an issue. Answers events would have a greater chance of being sent, but would contain less information. – Mike Bonnell May 24 '17 at 00:25
  • We don't need to catch exceptions, NSSetUncaughtExceptionHandler in appDelegate is enough, we would like a convenient way to use it with crashlytics that's all. – thibaut noah Oct 23 '17 at 12:52
  • @thibautnoah Sorry, I'm not following what you're looking for, can you share a bit more? – Mike Bonnell Oct 24 '17 at 12:04
  • @MikeBonnell basically something like this Crashlytics.sharedInstance().recordException(exception) – thibaut noah Oct 24 '17 at 12:19
  • Take a look at Crashlytics.h and specifically recordCustomExceptionName – Mike Bonnell Oct 24 '17 at 16:07
  • This will save your time `Crashlytics.sharedInstance().recordError(error as NSError, withAdditionalUserInfo: ["User_Id":0,"User_Name":"ABC","User_Email":"abc@gmail.com","User_Contact":"789797"])` – Urvish Modi May 31 '17 at 12:35
5

Finally Crashlytics added the desired feature 3.5.0!!

[CrashlyticsKit recordError:error];

or

Crashlytics.sharedInstance().recordError(error)

Reference

/**
 *
 * This allows you to record a non-fatal event, described by an NSError object. These events will be grouped and
 * displayed similarly to crashes. Keep in mind that this method can be expensive. Also, the total number of
 * NSErrors that can be recorded during your app's life-cycle is limited by a fixed-size circular buffer. If the
 * buffer is overrun, the oldest data is dropped. Errors are relayed to Crashlytics on a subsequent launch
 * of your application.
 *
 * You can also use the -recordError:withAdditionalUserInfo: to include additional context not represented
 * by the NSError instance itself.
 *
 **/
- (void)recordError:(NSError *)error;
- (void)recordError:(NSError *)error withAdditionalUserInfo:(nullable CLS_GENERIC_NSDICTIONARY(NSString *, id) *)userInfo;

https://docs.fabric.io/ios/changelog.html#january-7-2016


HISTORY

This actually doesn't work as I expected: The message is saved into Crashlytics but only after the app is restarted and it will only save the last message.

So far non of the solutions mentioned here works. There is no way to track handled exceptions in iOS using Crashlytics.


You can use this to log any exception

[[Crashlytics sharedInstance] recordCustomExceptionName:@"HandledException" reason:@"Some reason" frameArray:@[]];

In Crashlytics you'll see it in the crash report but with the NON-FATALS type.

Event if it's not it's intended usage exceptions are logged in the same way Android handled exceptions are.

This is available in version 3.0.7.

recordCustomExceptionName:reason:frameArray:

This method can be used to record a single exception structure in a report. This is particularly useful when your code interacts with non-native languages like Lua, C#, or Javascript. This call can be expensive and should only be used shortly before process termination. This API is not intended be to used to log NSException objects. All safely-reportable NSExceptions are automatically captured by Crashlytics.

https://docs.fabric.io/appledocs/Crashlytics/Classes/Crashlytics.html#//api/name/recordCustomExceptionName:reason:frameArray:

Community
  • 1
  • 1
7ynk3r
  • 958
  • 13
  • 17
3

It is not possible to log a caught exception in iOS using Crashlytics SDK. CLS_LOG can be used to log custom messages, but these log messages will go to Crashlytics only with the next crash data. If there is no crash, these log messages will never land in the Crashlytics dashboard. I got an official confirmation from the Crashlytics support team regarding the same. Logging caught exceptions in iOS is there in their roadmap.

Sinu Varghese
  • 800
  • 1
  • 14
  • 39
2

I have gone through different sites for this functionality to be supported for IOS alternate to Crashlytics.

I found crittercism is the best so far..@Dima I think it is the alternative to Crashlytics..try it.

Here are some links that are useful to integrate crittercism in your project...!

http://docs.crittercism.com/ios/ios.html#logging-handled-exceptions

http://www.raywenderlich.com/34050/overview-of-ios-crash-reporting-tools-part-2

@try {

     } 
@catch (NSException *exc) 
     {
        [Crittercism logHandledException:exc]
    }

Refer those links and see it is useful to you or not...!

Vidhyanand
  • 5,369
  • 4
  • 26
  • 59
1

Use below lines in catch block to handle custom caught exception

NSUncaughtExceptionHandler *handler = NSGetUncaughtExceptionHandler();
handler(exception);

as explained for iOS http://support.crashlytics.com/knowledgebase/articles/222764-can-i-use-a-custom-exception-handler

Aanabidden
  • 375
  • 6
  • 18
  • Could you pls provide the full code with API call. Please consider the below situation. @ try { // lines of code which can cause an exception } @ catch (NSException *exception) { NSLog(@"%@", exception.reason); // report to Crashlytics } – Sinu Varghese May 12 '15 at 07:17
  • @SinuVarghese try-catch block for reporting can be like below @ try { // lines of code } @ catch (NSException *exception) { NSUncaughtExceptionHandler *handler = NSGetUncaughtExceptionHandler(); handler(exception); } – Aanabidden May 12 '15 at 07:24
  • the above code just saving the exception in a handler. But what I want is 'see the caught exception in Crashlytics dashboard'. – Sinu Varghese May 12 '15 at 08:06
  • @SinuVarghese no, the exception is not saved its being passed to the default exception handler that we set as crashalytics in applaunching method – Aanabidden May 15 '15 at 16:09
  • I tried this approach, but the handler was always nil. Anyone have luck with it? – markdb314 Jul 24 '15 at 01:40
  • @markdb314 then crashlytics might not be initialised. Because handler does not shows nil in my code. – Aanabidden Aug 04 '15 at 09:11