6

Bugsnag docs › Platforms › Android › Customizing breadcrumbs

 2 years ago
source link: https://docs.bugsnag.com/platforms/android/customizing-breadcrumbs/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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. A configurable number of breadcrumbs are attached to each error report to help diagnose what events led to the error.

This documentation is for version 5 of the Bugsnag Android notifier. If you are using older versions, we recommend upgrading to the latest release using our Upgrade guide. Documentation for the previous release can be found on our legacy pages.

Automatic breadcrumbs#

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

  • Activity Lifecycle callbacks
  • Network connectivity changes
  • Bluetooth connectivity changes
  • Battery state changes
  • Device rotation
  • Media Scanner events
  • Telephony events
  • Other device metrics and more

This can be controlled using the enabledBreadcrumbTypes configuration option.

Capturing OkHttp network requests#

Bugsnag can capture network requests from the OkHttp library as breadcrumbs. These are attached to each error report to help diagnose what events led to the error:

Android Network Breadcrumb screenshot

To capture network breadcrumbs, add bugsnag-plugin-android-okhttp as a dependency to your project:

dependencies {
    implementation "com.bugsnag:bugsnag-android:5.+"
    implementation "com.bugsnag:bugsnag-plugin-android-okhttp:5.+"
    implementation "com.squareup.okhttp3:okhttp:4.+"
}

Bugsnag supports both OkHttp 3 and 4.

Then configure Bugsnag to start with BugsnagOkHttpPlugin:

val bugsnagOkHttpPlugin = BugsnagOkHttpPlugin()
val config = Configuration.load(this)
config.addPlugin(bugsnagOkHttpPlugin)
Bugsnag.start(this, config)

Finally, register the BugsnagOkHttpPlugin instance to listen to events for all OkHttpClient objects in your app:

val okHttpClient = OkHttpClient.Builder()
    .eventListener(bugsnagOkHttpPlugin)
    .build()

The OkHttp response body must be closed for breadcrumbs to be captured. Failing to close the body is an error that will leak resources within OkHttp.

For performance reasons, the value reported for responseContentLength in breadcrumb metadata will be 0 if the OkHttp response body has not been read.

Adding manual breadcrumbs#

Append manual breadcrumbs with a message via the Bugsnag client:

Bugsnag.leaveBreadcrumb("App loaded")
Bugsnag.leaveBreadcrumb("User clicked a button")

Bugsnag will keep track of the time and order of the breadcrumbs, and show them 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:

val metadata = mapOf(
  "from" to "moka",
  "to" to "french press"
)
Bugsnag.leaveBreadcrumb("Preference updated", metadata, BreadcrumbType.STATE)

Breadcrumb “types” can be used to differentiate different types of events, such as user activity and changes in application state. See the BreadcrumbType enumeration for a complete list of the breadcrumb types available to Java or Kotlin code, and the Native API header for C/C++ code. Your breadcrumbs will not be affected by the enabledBreadcrumbTypes configuration value.

Tracking fragment lifecycles#

If you wish to log breadcrumbs for the Fragment Lifecycle, we suggest that you use FragmentLifecycleCallbacks for all the activities which you wish to track.

Example: Using FragmentLifecycleCallbacks to log navigation events

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.

val config = Configuration.load(this)
config.addOnBreadcrumb(OnBreadcrumbCallback { breadcrumb ->
    if (breadcrumb.message == "Noisy breadcrumb") {
        false // ignore the breadcrumb
    } else {
        true // capture the breadcrumb
    }
}
Bugsnag.start(this, 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:

val cb = OnBreadcrumbCallback { /* ... */ }
Bugsnag.addOnBreadcrumb(cb)
// ...
Bugsnag.removeOnBreadcrumb(cb)

The Breadcrumb class#

The following information is available on the Breadcrumb class, the representation of breadcrumb information available in an OnBreadcrumbCallback. See the Breadcrumb class for full documentation.

property type description message String The description of the breadcrumb metadata Map<String, Any?> Diagnostic data relating to the breadcrumb timestamp Date The timestamp that the breadcrumb was left type BreadcrumbType The type of breadcrumb left


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK