Skip to content

Commit 43adb6e

Browse files
authored
[#106] Sentry reports use message instead of exception names for titles
* 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.
1 parent 679c058 commit 43adb6e

File tree

5 files changed

+47
-31
lines changed

5 files changed

+47
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The format is based on the Steamclock [Release Management Guide](https://github.
77

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

1112
---
1213

app/build.gradle

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ dependencies {
4949
implementation project(':steamclog')
5050
// Since Sentry is a dependency of steamclog, we do not have to import it again
5151

52-
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'
53-
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
52+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.2'
5453
implementation 'androidx.appcompat:appcompat:1.4.1'
55-
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
56-
implementation 'com.google.android.material:material:1.5.0'
54+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
55+
implementation 'com.google.android.material:material:1.6.1'
5756
}

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
buildscript {
3-
ext.kotlin_version = '1.5.30'
3+
ext.kotlin_version = '1.7.10'
44
ext.timber = '5.0.1'
5-
ext.sentry = '5.7.1'
5+
ext.sentry = '6.4.0'
66

77
repositories {
88
google()

steamclog/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ android {
5757

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

6364
implementation "com.jakewharton.timber:timber:$timber"

steamclog/src/main/java/com/steamclock/steamclog/Destinations.kt

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package com.steamclock.steamclog
22

33
import android.annotation.SuppressLint
44
import android.util.Log
5+
import io.sentry.Breadcrumb
56
import io.sentry.Sentry
7+
import io.sentry.SentryLevel
68
import timber.log.Timber
79
import java.io.File
810
import java.text.SimpleDateFormat
@@ -41,35 +43,48 @@ internal class SentryDestination : Timber.Tree() {
4143
val originalMessage = wrapper?.originalMessage ?: message
4244
val originalThrowable = wrapper?.originalThrowable
4345

44-
// Always add breadcrumb that indicates the log message.
45-
Sentry.addBreadcrumb(generateSimpleLogMessage(
46-
priority,
47-
includeEmoji = false,
48-
wrapper,
49-
message
50-
), breadcrumbCategory)
46+
// Create a full log message and include as a breadcrumb to Sentry.
47+
val verboseLogMessage = generateSimpleLogMessage(priority, includeEmoji = false, wrapper, originalMessage)
48+
Sentry.addBreadcrumb(createSentryBreadcrumbFor(priority, verboseLogMessage))
5149

52-
when {
53-
priority == Log.ERROR && originalThrowable != null -> {
54-
// Check to see if we want to allow or block the Throwable from being reported
55-
// as an error.
50+
// Log error report if desired.
51+
if (priority == Log.ERROR) {
52+
if (originalThrowable != null) {
53+
// Check to see if we want to block this Throwable from being logged as an Error.
5654
if (SteamcLog.config.filtering.shouldBlock(originalThrowable)) {
57-
Sentry.addBreadcrumb("${originalThrowable::class.simpleName} on blocked list, and has " +
58-
"been blocked from being captured as an exception: " +
59-
"${originalThrowable.message}", breadcrumbCategory)
60-
} else {
61-
Sentry.captureException(originalThrowable)
55+
Sentry.addBreadcrumb(
56+
"${originalThrowable::class.simpleName} on blocked list, and has " +
57+
"been blocked from being captured as an exception: " +
58+
"${originalThrowable.message}", breadcrumbCategory
59+
)
60+
return
6261
}
62+
// Log stack trace as a breadcrumb (this logged as an INFO breadcrumb by default)
63+
Sentry.addBreadcrumb(originalThrowable.stackTraceToString(), breadcrumbCategory)
6364
}
64-
priority == Log.ERROR -> {
65-
// If no throwable given, capture message as the error
66-
Sentry.captureMessage(originalMessage)
67-
}
68-
else -> {
69-
// Not an error; breadcrumb should already be added, so we do not
70-
// need to do anything more.
71-
}
65+
66+
// Always use the message as our error report, as this gives us way more contextual
67+
// info about the problem than the name of the Exception.
68+
Sentry.captureMessage(originalMessage, SentryLevel.ERROR)
69+
}
70+
}
71+
72+
/**
73+
* Takes the given priority level and creates a breadcrumb at a similar level. Mappings
74+
* aren't 1 to 1, but this should still give us better detail in our breadcrumb log.
75+
*/
76+
private fun createSentryBreadcrumbFor(priority: Int, message: String): Breadcrumb {
77+
val breadcrumb = when (priority) {
78+
Log.ASSERT -> Breadcrumb.error(message)
79+
Log.ERROR -> Breadcrumb.error(message)
80+
Log.WARN -> Breadcrumb.info(message)
81+
// Log.INFO is the default
82+
Log.DEBUG -> Breadcrumb.debug(message)
83+
Log.VERBOSE -> Breadcrumb.debug(message)
84+
else -> Breadcrumb.info(message)
7285
}
86+
breadcrumb.category = breadcrumbCategory
87+
return breadcrumb
7388
}
7489
}
7590

0 commit comments

Comments
 (0)