Skip to content

Commit

Permalink
[#106] Sentry reports use message instead of exception names for titles
Browse files Browse the repository at this point in the history
* Updated library dependencies
* Sentry logging improvements
* Always log error using message (not exception); this promotes having error reports with more contextual information
* Sentry Breadcrumb levels now attempt to match the logged priority level (previously all being logged as "info"); this allows us better filtering on Sentry.
  • Loading branch information
ssawchenko authored Aug 30, 2022
1 parent 679c058 commit 43adb6e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The format is based on the Steamclock [Release Management Guide](https://github.

- Updated libraries including Sentry (#101)
- Replaced kotlin synthetics with ViewBinding in sample app (#47)
- Update kotlin version, and dependencies again (part of #106)

---

Expand Down
7 changes: 3 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ dependencies {
implementation project(':steamclog')
// Since Sentry is a dependency of steamclog, we do not have to import it again

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.2'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.android.material:material:1.6.1'
}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.5.30'
ext.kotlin_version = '1.7.10'
ext.timber = '5.0.1'
ext.sentry = '5.7.1'
ext.sentry = '6.4.0'

repositories {
google()
Expand Down
3 changes: 2 additions & 1 deletion steamclog/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ android {

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
// https://blog.jetbrains.com/kotlin/2020/07/kotlin-1-4-rc-released/
// No longer need to include kotlin stdlib dependency
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

implementation "com.jakewharton.timber:timber:$timber"
Expand Down
63 changes: 39 additions & 24 deletions steamclog/src/main/java/com/steamclock/steamclog/Destinations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.steamclock.steamclog

import android.annotation.SuppressLint
import android.util.Log
import io.sentry.Breadcrumb
import io.sentry.Sentry
import io.sentry.SentryLevel
import timber.log.Timber
import java.io.File
import java.text.SimpleDateFormat
Expand Down Expand Up @@ -41,35 +43,48 @@ internal class SentryDestination : Timber.Tree() {
val originalMessage = wrapper?.originalMessage ?: message
val originalThrowable = wrapper?.originalThrowable

// Always add breadcrumb that indicates the log message.
Sentry.addBreadcrumb(generateSimpleLogMessage(
priority,
includeEmoji = false,
wrapper,
message
), breadcrumbCategory)
// Create a full log message and include as a breadcrumb to Sentry.
val verboseLogMessage = generateSimpleLogMessage(priority, includeEmoji = false, wrapper, originalMessage)
Sentry.addBreadcrumb(createSentryBreadcrumbFor(priority, verboseLogMessage))

when {
priority == Log.ERROR && originalThrowable != null -> {
// Check to see if we want to allow or block the Throwable from being reported
// as an error.
// Log error report if desired.
if (priority == Log.ERROR) {
if (originalThrowable != null) {
// Check to see if we want to block this Throwable from being logged as an Error.
if (SteamcLog.config.filtering.shouldBlock(originalThrowable)) {
Sentry.addBreadcrumb("${originalThrowable::class.simpleName} on blocked list, and has " +
"been blocked from being captured as an exception: " +
"${originalThrowable.message}", breadcrumbCategory)
} else {
Sentry.captureException(originalThrowable)
Sentry.addBreadcrumb(
"${originalThrowable::class.simpleName} on blocked list, and has " +
"been blocked from being captured as an exception: " +
"${originalThrowable.message}", breadcrumbCategory
)
return
}
// Log stack trace as a breadcrumb (this logged as an INFO breadcrumb by default)
Sentry.addBreadcrumb(originalThrowable.stackTraceToString(), breadcrumbCategory)
}
priority == Log.ERROR -> {
// If no throwable given, capture message as the error
Sentry.captureMessage(originalMessage)
}
else -> {
// Not an error; breadcrumb should already be added, so we do not
// need to do anything more.
}

// Always use the message as our error report, as this gives us way more contextual
// info about the problem than the name of the Exception.
Sentry.captureMessage(originalMessage, SentryLevel.ERROR)
}
}

/**
* Takes the given priority level and creates a breadcrumb at a similar level. Mappings
* aren't 1 to 1, but this should still give us better detail in our breadcrumb log.
*/
private fun createSentryBreadcrumbFor(priority: Int, message: String): Breadcrumb {
val breadcrumb = when (priority) {
Log.ASSERT -> Breadcrumb.error(message)
Log.ERROR -> Breadcrumb.error(message)
Log.WARN -> Breadcrumb.info(message)
// Log.INFO is the default
Log.DEBUG -> Breadcrumb.debug(message)
Log.VERBOSE -> Breadcrumb.debug(message)
else -> Breadcrumb.info(message)
}
breadcrumb.category = breadcrumbCategory
return breadcrumb
}
}

Expand Down

0 comments on commit 43adb6e

Please sign in to comment.