Customizing breadcrumbs

In order to understand what happened in your application before each crash, it can be helpful to leave short log statements that we call breadcrumbs. Breadcrumbs are attached to the reported event to help diagnose what led to the error.

Automatic breadcrumbs

By default, Bugsnag captures breadcrumbs for common actions and device changes, including:

  • Low memory warnings
  • Device rotation
  • Menu presentation
  • Screenshot capture (not the screenshot itself)
  • Undo and redo
  • Table view selection
  • Window visibility changes
  • Non-fatal errors

This can be controlled using the enabledBreadcrumbTypes configuration option.

Adding manual breadcrumbs

Append manual breadcrumbs with a message via the Bugsnag client:

[Bugsnag leaveBreadcrumbWithMessage:@"App loaded"];
[Bugsnag leaveBreadcrumbWithMessage:@"User clicked a button"];
Bugsnag.leaveBreadcrumb(withMessage: "App loaded")
Bugsnag.leaveBreadcrumb(withMessage: "User clicked a button")

The notifier tracks the time when a breadcrumb is logged, and shows the message and timestamp on your dashboard.

Attaching metadata

Additional data can be attached to breadcrumbs by providing the additional metadata argument. Metadata will be presented on the Bugsnag dashboard alongside the breadcrumb name and type:

[Bugsnag leaveBreadcrumbWithMessage:@"Preference updated"
                           metadata:@{@"from": @"moka", @"to": @"french press"}
                            andType:BSGBreadcrumbTypeState];
Bugsnag.leaveBreadcrumb("Preference updated", 
                        metadata: ["from": "moka", "to": "french press"],
                        type: .state)

Breadcrumb “types” can be used to differentiate different types of events, such as user activity and changes in application state. See the BSGBreadcrumbType enumeration for a complete list of the breadcrumb types available. Your breadcrumbs will not be affected by the enabledBreadcrumbTypes configuration value.

Capturing NSNotification events

To add breadcrumbs for every instance of a NSNotification being sent, use leaveBreadcrumbForNotificationName:

[Bugsnag leaveBreadcrumbForNotificationName:@"XYZControllerPushed"];
Bugsnag.leaveBreadcrumb(forNotificationName: "XYZControllerPushed")

This can be useful for instrumenting your own notification pipelines for notable events more quickly.

Discarding and amending breadcrumbs

You can register a callback that is executed each time a breadcrumb is captured using addOnBreadcrumb. This can be helpful if you wish to filter out certain automatic breadcrumbs from your application or amend the data contained within them.

BugsnagConfiguration *config = [BugsnagConfiguration loadConfig];
[config addOnBreadcrumbBlock:^(BugsnagBreadcrumb *_Nonnull breadcrumb) {
    if ([breadcrumb.message isEqualToString:@"Noisy breadcrumb"]) {
        return NO;
    } else {
        return YES;
    }
}];
[Bugsnag startWithConfiguration:config];
let config = BugsnagConfiguration.loadConfig()
config.addOnBreadcrumb { (breadcrumb) -> Bool in
    if (breadcrumb.message == "Noisy Breadcrumb") {
        return false
    } else {
        return true
    }
}
Bugsnag.start(with: config)

Adding and removing callbacks

We recommend adding callbacks through the addOnBreadcrumb configuration option to ensure that it is registered as soon as Bugsnag starts. However, the following methods are provided to allow callbacks to be added and removed whilst the application is running:

BugsnagOnBreadcrumbBlock cb = ^BOOL(BugsnagBreadcrumb *breadcrumb) { /* ... */ };
[Bugsnag addOnBreadcrumbBlock:cb];
// ...
[Bugsnag removeOnBreadcrumbBlock:cb];
func cb(breadcrumb: BugsnagBreadcrumb) -> Bool { /* ... */ }
Bugsnag.addOnBreadcrumb(block: cb)
// ...
Bugsnag.removeOnBreadcrumb(block: cb)

The BugsnagBreadcrumb class

The following information is available on the BugsnagBreadcrumb class, the representation of breadcrumb information available in a breadcrumb callback block.

property type description
message String The description of the breadcrumb
metadata Dictionary Diagnostic data relating to the breadcrumb
timestamp Date The timestamp that the breadcrumb was left
type BreadcrumbType The type of breadcrumb left